ITERATIONS IN C#
LOOPS IN C SHARP THE WHILE LOOP
In C Sharp loops the syntax of a while loop is as follows:
while (boolean condition)
{
//INSTRUCTIONS
}
The Boolean condition is a normal expression that evaluates to true or false. It is important to note that if the Boolean condition is false before starting the loop (the condition is checked first) then the while loop will never be executed. While iterates through the block of code enclosed in the braces as long as the condition is true. Let’s see an example:
byte b = 0;
while (b<5)
{
Console.WriteLine(b);
b++;
}
The loop prints the values (0 1 2 3 4). If we forgot to increment b at the end of the loop with b++ we would have an infinite loop which would lead to a program crash as the false end-of-loop condition would never be reached.
LOOPS IN C SHARP THE DO WHILE LOOP
In C Sharp loops the syntax of the do-while loop is as follows:
do
{
//INSTRUCTIONS
} while (boolean condition)
The variant with respect to the while loop is that the loop is executed at least once since the Boolean condition is checked at the end of the code block.
byte b = 0;
do
{
Console.WriteLine(b);
b++;
} while (b>0 && b<=5);
It will print on screen (0 1 2 3 4 5). Questions What happens in this case if we omit b++? What will be printed on the screen?
LOOPS IN C SHARP THE FOR LOOP
The syntax of the for loop is as follows:
for (initial condition; test condition; end-of-loop instruction)
{
//INSTRUCTIONS
}
As you can see, the for loop is formed in the header by three parts separated by a semicolon. The initial condition in which a control variable is generally declared and initialized, the test condition represented by a Boolean condition and the end-loop instruction usually represented by the increase or decrease of the control variable specified in the initial condition. Let’s see an example.
for(int i=0; i<5; i++)
{
Console.WriteLine(i);
}
The for statement will print in this case (0 1 2 3 4).
BREAK AND CONTINUE STATEMENTS
In all the cycles seen so far it is possible to specify break and continue statements. With break we immediately interrupt the loop and the program flow passes to the instruction following the loop in question, with continue we interrupt the code block in the loop executed up to that point and we go back to the loop header after increasing the ready to perform a new iteration. Suppose we want to print all the even numbers that are in the range from 0 to 15.
for(int i=0; i<15; i++)
{
if (i%2 == 0)
Console.WriteLine(i);
else
continue;
}
It will print on screen (0 2 4 6 8 10 12 14).
Now suppose we want to exit the loop if a certain variable takes the value 5.
int y = 0;
for(int i=0; i<15; i++)
{
if (y == 5)
break;
else
Console.WriteLine(i);
y++
}
Will print (0 1 2 3 4).
THE FOREACH LOOP
The last cycle to examine is the foreach. This type of loop is used to iterate objects. Executes a statement or block of statements for each element in an instance of a type. For example:
string myVar = “pippo”;
foreach(char c in myVar)
{
Console.WriteLine(c);
}
It will print all the individual characters found in the string foo.
LINKS TO PREVIOUS POST
VISUAL STUDIO CODE
using System; namespace Iterazioni; internal class Program { private static void Main(string[] args) { //CICLO WHILE byte b = 0; while (b < 5) { Console.WriteLine(b); b++; } byte z = 0; //CICLO DO WHILE do { Console.WriteLine(z); z++; } while (z > 0 && z <= 5); //CICLO FOR for (var i = 0; i < 5; i++) Console.WriteLine(i); //L’istruzione for stamperà in questo caso (0 1 2 3 4). //LE ISTRUZIONI BREAK E CONTINUE for (var i = 0; i < 15; i++) if (i % 2 == 0) Console.WriteLine(i); else continue; //INCREMENTA i E TORNA IN TESTA AL CICLO var y = 0; for (var i = 0; i < 15; i++) { if (y == 5) break; //INTERROMPE IMMEDIATAMENTE IL CICLO. Console.WriteLine(i); y++; } // IL CICLO FOREACH var myVar = "pippo"; foreach (var x in myVar) Console.WriteLine(x); } }
Leave A Comment