C# Basics

C# while loop

The C# while loop repeatedly executes a block of code inside the loop while the given logical expression is true; otherwise, it breaks. This type of loop is useful when the number of loop iterations does not need to be known before the loop begins, and a counter variable is not needed. Any variable initialization and updating needs to be managed by you.

Bear in mind that somewhere in the loop, the expression should be changed to false to avoid an infinite or endless loop.

The structure of a C# while loop is as follows:
C# while loop

C# while loop execution order C# while loop flow diagram
C# while loop execution order C# while loop flow chart

When the while loop is first encountered, the bool expression is evaluated. If the expression is true, the code within the block is executed. When the block of code reaches the closing brace, control returns to the top, and the whole process starts over again. If the condition is false the first time the while loop is encountered, then the code within the braces is never executed.

The C# while loop starts with the while keyword followed by a single expression within parentheses.

  • Loop takes only one expression.
  • The condition is only evaluated at the beginning of each iteration.
  • The condition (Boolean expression) is evaluated before each new iteration of the loop. This must be true in order for the next iteration to be performed. The iterations end when the condition is false.
  • If the loop’s condition is constantly true, the loop never ends.
  • Set a Boolean flag to false somewhere in the loop to avoid an infinite or endless loop.
  • Loop ends with a semicolon “;”.
  • Statements are the block of code that you want to execute multiple times in a loop.

while Loop Example

The code below shows an example of how a C# while loop works. It writes out all the integers from 1 to 4. If you have more than one statement within the loop, you must enclose the statements in curly brackets.

while loop example

Run Demo

The above code consists of:

  1. An initialization and declaration of a variable counter with the int type and assigns an initial value of 0 to it, counter=0.
  2. The Boolean expression is evaluated, and if it is true, then the sequence of operations in the body of the loop is executed.
  3. A Boolean condition (counter < 4) will repeatedly execute until the counter is less than 4.
  4. An expression for updating the counter (counter++ is a syntactically-shorter version of counter=counter+1) adds 1 at the end of each repetition, thus increasing the counter variable by 1.
OUTPUT

1
2
3
4

With a Single Statement

Curly brackets for the loop are optional if there is only one statement in the code block.

int counter = 0;
while (counter < 4)
  counter++;

Loop Control Statements

You may need to use jump statements to control the order in which the statements are executed. Read more on …

C# Reference | Microsoft Docs


Tagged in :

Add Comment

Click here to post a comment