While loops

While loops are very similar to For loops, They are defined like this:

while([conditions to be checked])
[Code to execute]

This allows you to continuously repeat a section of code while a condition is satisfied.


int n = 0;

while( n == 0)
    Console.WriteLine("N is 0");

This would execute once as N is changed from zero the first time it runs. Unlike for loops, while loops do not allow you to declare or assign the variable used in the conditions.

The same control statements (break and continue) as are used in for loops can also be used in while loops.


Print X to the console N time using a while loop

Copyright © learncs.org. Read our Terms of Use and Privacy Policy