I am going to explain these loops in C#, however, with a little bit of research, you can find the syntax for any other languages.
Loops in C#
Loops allow you to run the same code multiple times in a row. Loops, too, like if Statements use curly braces to enclose all of the code of the loops. There are several different types of loops in C#:
- for loops
- while loops
- do-while loops
for loop
The for loop is used when you know ahead of time how many times you want to run a loop. It consists of an initialization statement and a loop body. The loop body is where all the code of the loop is run. The initialization statement has three parts:
- A declaration (Creating a variable to be used in the condition)
- A condition (If this condition is true, the loop will run again)
- An updater (A statement that will run every time after the body code is run.)
for (int i = 0; i < 6; i++) { // 'i++' is the exact same as saying 'i = i + 1'.
Console.WriteLine("#" + i); // I am able to use the variable 'i' here.
Console.WriteLine("Goodbye World!");
}
Console.WriteLine("i is equal to: " + i); // CS0103: The name 'i' does not exist in the current context
/*
The above statement errors because the variable 'i' was only defined inside
of the for loop, not outside.
*/
while loop
The while loop is used when you do NOT know ahead of time how many times you want to run a loop. It consists of only a condition and a loop body. Like the for loop, the loop body is where all the code of the loop is run. The condition is any boolean expression that evaluates to either true or false.
int i = 1;
while (i <= 5) {
Console.WriteLine(i);
i++; // Without this line, 'i' would never reach the value of 5, therefore getting stuck in an infinite loop.
}
do-while loop
The do-while loop is very similar to the while loop in that it executes a block of code if an expression evaluates to true. The only difference is that while the while loop starts by checking the expression, and THEN running the body code, the do-while loop begins by running the code, and THEN checking the expression, looping back if it is true. This guarantees that the code will run at least once.
int CorrectNumber = 6;
int GuessedNumber; // Here we leave the variable uninitialized. We will change that later.
Console.WriteLine("I'm thinking of a number from 1 to 10.");
Console.WriteLine("Enter a guess, or type 0 to give up.");
do {
Console.Write("Your guess: "); // 'Write' is similar to 'WriteLine', except that it doesn't create a new line.
GuessedNumber = Convert.ToInt32(Console.ReadLine());
if (GuessedNumber == 0) break; // 'break' is a special keyword that BREAKS out of a loop and will stop it from repeating again.
// If the if-statement body is only one line, you can omit the curly braces as is demonstrated above.
} while (GuessedNumber != CorrectNumber); // '!=' is the NOT EQUAL TO symbol.
if (GuessedNumber != 0) {
Console.WriteLine("Congratulations! The correct number was " + CorrectNumber);
} else {
Console.WriteLine("Goodbye!!!");
}