while Loop and do while Loop in C# With Examples

Description:

while loop and do while loop in c#: in this article, we will discuss a very important loop which is while and do while loop in very detail and with examples



what is Loops:

Loops are used to execute an instruction or a block of instructions several times in a row.

while loop in C#

It is the mother of all loops, the structure of which shows the basic principles of how Can illustrate loops well.

Each loop consists of a keyword (in the case of the while loop in c# the keyword while), one Condition that determines whether the loop should be executed, and a block of statements that iterates should be carried out:

If there is only one statement to be executed, the curly brackets can be omitted:

Note: For C ++ programmers: C# only allows expressions as “condition” that return a Boolean value. Numeric expressions cause a compiler complaint because int values ​​in C# are not automatically converted to bool values being transformed.


The syntax of the while loop in c# only differs from the structure of an if-Condition. However, while the statement (or the statement block) of an if-condition when fulfilled Condition only executed once and then the application with the following under the if-condition Statement is continued, the application jumps back after executing a loop block to the loop condition to check it again. This way, the statement block becomes the loop executed again and again as long as the condition evaluates to true. For the programmer, it follows that he has to set up the loop in such a way that it not only runs when necessary but above all is canceled again.

Usually, a loop variable is set up for this purpose, which controls the execution of the loop and which usually has a short name: for example i or n. This loop variable becomes

  • initialized before the loop (in the example above n = 0;)
  • evaluated in the loop condition (n <= 10)
  • changed in the loop (the variable is usually incremented: ++ n, but you can also decrement it or change their value in any other way).

By combining these three statements, you can specify how many times the loop repeats. This becomes clear when one understands how such a loop is executed. In consideration of the instructions for the loop variable, the structure of a while loop looks like this:

While Loop execution in C#:

In the code above, the loop variable n is initially set to 0. Then the loop begins. At the start, the loop is the loop condition. As long as this condition is met (returns true), the loop will be executed repeatedly. Since n is initially equal to 0, as I said, and the loop condition checks whether n is smaller equals 10, the condition is fulfilled and the statement block of the loop is executed (man speaks of iteration here). The loop variable is incremented within the statement block, see above that it now has the value 1. Finally, the statement block is completely processed. The program execution now jumps back to the loop condition, which is checked again. Since 1 always is still less than or equal to 10, the loop is executed again and so on until the 11th processing of the Loop variable n is set to 11. After that, the loop condition is no longer met and the code continues with the first statement after the loop. Overall there is the loop thus the powers of two up to 1024 (equal to 210).


Use of the variable loop in the statement block:

Loops execute the same code several times in a row. It would be pretty boring though if this code would produce exactly the same result every time it was executed (iterated). The Most loops therefore use variables within their statement block whose values ​​differ from Change iteration to iteration. The loop variable is of course literally ideal for this, because the programmer can and must determine for them in which steps their value differs from which initial value up to which end value changed:

Endless while loop in C#:

It is important that when you put on a loop you make sure that it is really left again. Consider the following loop:

Here, the while loop variable n is decremented, i.e. reduced by 1, after each loop pass. The value of n will never be greater than 10, and it will never exit the loop!

Such endless loops are a common example of logical errors within a syntactically correct one Application. Loops that work correctly after a hundred program calls are particularly tricky degenerate into an endless iteration at the 101st program call.

For example, the following loop only becomes an endless loop if the variable gain before entering the loop has been assigned a negative value.



do … while loop in C#:

The do … while loop in C#  is very similar to the while loop. The only major difference is that the Loop condition is checked not at the beginning but at the end of the loop. That’s why the do … while loop in C# performs at least one iteration. This is why it is also known as the “accepting” loop.

Note: Pay attention to the semicolon after the condition! The do … while loop is the only loop where the Condition is a semicolon. (The semicolon here ends the do … while statement, while the other loops automatically terminated by the following statement or the following statement block.)

Programming Examples of while and do-while loops in c#:

Example1: how to use simple while loop in c#:

while loop in c#


Example2: how to use break in while loop in c#:

while loop in c#

Example3: how to use nested while loop in c#:

while loop in c#


Example4: how to make multiplication table of 2 using do-while loop in c#:

while loop in c#



Example5: how to make menu application using do-while loop in c#:

The do … while loop in c#  is particularly useful for outputting menus for console applications. The following application is a revision of my previous article example. The console menu is no longer just when starting the application, but also after selecting and processing the individual commands. Only when the user selects the menu command to exit is the do … while loop exited and the application terminates.

while loop in c#

while loop in c#

while loop in c#

Recommended For You

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: