While loop, do-while C#
Blog > C# > While loop, do-while C#

While loop, do-while C#


While loop, do-while C#

Next to the for loop in C# there are also while and do-while loops (while, do-while C# loop). They will be described together because they differ only slightly. The loops allow you to execute the same code many times based on the given conditions. The following entry describes the while and do-while loops with their example applications.


Syntax of while loops

The while loop has a simple syntax. At the beginning, after the word while appears a condition that must be met in order for the loop to be executed. A repeated string of code is in braces, as long as the condition returns true. The condition is checked after each execution of all lines of code in the clamps. If one of the elements of the condition being checked is a variable, it must be declared and initialized before the loop appears.

int i = 0;//Necessary declaration and initiation before the loop

while (i < 6) //Condition - as long as it is fulfilled, the loop is executed
  Console.WriteLine("A value less than 6");

Syntax of do-while loops

The do-while loop is similar in action to the while loop, and its basic difference is that regardless of the condition, it will be done at least once.

int i;//Necessary declaration before the loop

  Console.WriteLine("A value less than 6 or the first loop iteration");
} while (i < 6);//Condition - as long as it is fulfilled, the loop is executed

The variable that we want to place in the condition must be declared before the occurrence of the loop in the code. Variable initialization may only appear in the loop because it will still be before the condition is checked.

Application of the while loop

All loops can be used interchangeably in any operation that requires repeating the code. Below is an example of a while loop that requires interaction with the user.

int ileRazy;//Declaration of the variable before the loop

Console.WriteLine("How many times to display the inscription? ");
ileRazy = int.Parse(Console.ReadLine());//int.Parse converts numbers entered as a string into an int type

while( ileRazy > 0 )//It will display as long as the variable ileRazy is greater than 0.
  Console.WriteLine("I display the inscription.");
  ileRazy--;//In each iteration it decreases the value of the variable

Application of the do-while loop

The do-while loop works, for example, for displaying menus in console programs. The first loop iteration informs the user about possible choices, and the next ones depend on the choice made. An example of such use is below:

int wybor;

  Console.WriteLine("Choose the operation number? The number 0 ends the loop.");
  wybor = int.Parse(Console.ReadLine());

  //Operations for the selected 1

  //Operations for the selected 2

} while (wybor != 0);

Loop while compared to the for loop

The comparison has already appeared when discussing the for loops, but it is worth recalling the principle in another example.

//Loop while
int i = 20;
while(i > 10)
  //Repeated program code

//Pętla for
for (int i = 20; i > 10; i--)
  //Repeated program code

Any questions?