C# Basics: Loops Part 3

The next loop types are the while loop, and the do while loop.

We can use a while loop as long as we have a boolean condition that remains true. Fair warning: it would be wise to make sure the condition isn’t always true, otherwise the infinite looping will crash your program.

If you do need a loop to continue on for eternity, make use of “yield return new waitforseconds” command to make sure your code has time to breathe.

Syntax of a while loop:

Now the do while loop will have the condition at the end of the looping code, instead of at the beginning like the while loop does. This is so that even if the condition is false, the code will run. The code won’t start again until the condition becomes true, so at the very least, the code will be activated once.

Syntax of the do while loop:

In this one, i will end up being equal to 1, even if the condition was i>5 instead of i<5.