Module-4-loop
Module-4-loop
statements
By: Marygrace T. Agpaoa
Loops
Syntax
while (condition)
{
// code block to be executed
}
Example
int i = 0; 0 1 2 3 4 5
while (i < 5)
{
0 1 2 3 4
Console.WriteLine(i);
i++; 1 2 3 4 5
}
• Note: Do not forget to increase the variable
used in the condition, otherwise the loop will
never end!
DIT
• Syntax
foreach (type variableName in arrayName)
{
// code block to be executed
}
Example
• C#Break
• It was used to "jump out" of a switch
statement.
int i = 0;
while (i < 10)
{
if (i == 4)
{
i++;
continue;
}
Console.WriteLine(i);
i++;
}
C# Arrays
• Create an Array
• Arrays are used to store multiple values
in a single variable, instead of declaring
separate variables for each value.