The while loop

The while keyword is used to create while loop in C#. The syntax for while loop is:

New Project (80)

The Code:

New Project (83).png

The Output of Your Program:

New Project (84)

Notes:

C# while loop consists of a test-expresion. If the test-expresion is evaluated to true, statements inside the while loop are executed. After execution, the test-expresion is evaluated again. If the test-expression is evaluated to false, the while loop terminates.

 

The do…while Loop

The do and while keyword is used to create a do…while loop. It is similar to a while loop, however there is a major difference between them.

 

In while loop, the condition is checked before the body is executed. It is the exact opposite in do…while loop, i.e. condition is checked after the body is executed.

 

This is why, the body of do…while loop will execute at least once irrespective to the test-expression.

New Project (81).png

The Code:

New Project (85)

The Output of Your Program:

New Project (86).png

Prev Lesson:                          Next Lesson:

The switch Statement      The for loop