Loop
statements
By: Marygrace T. Agpaoa
Loops
• Loops can execute a block of code as
long as a specified condition is reached.
• Loops are handy because they save
time, reduce errors, and they make code
more readable.
while
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
• Write a program that accepts name and
display it 10 times.
• Write a program that accepts 10
numbers using while statement and
display the sum and average of the
numbers.
The Do/While Loop
• The do/while loop is a variant of the while loop. This
loop will execute the code block once, before
checking if the condition is true, then it will repeat
the loop as long the condition is true.
Syntax
do
{
// code block to be executed
}
while (condition);
Example
int i = 0;
do
{ 0 3 4
Console.WriteLine(i);
1 2 3 4 5
i++;
} 1 2 3 4 5
while (i < 5);
Do not forget to increase the variable used in the
condition, otherwise the loop will never end!
Searwork
• Write a program that display a menu for
addition, subtraction, multiplication and
division. When an operation is selected,
it will prompt the user to enter 2
integers. The program should display
the result of the operation. Use do
while.
Sample output:
C# For Loop
• When you know exactly how many times
you want to loop through a block of code,
use the for loop instead of a while loop:
• Syntax
for (statement 1; statement 2; statement 3)
{
// code block to be executed
}
• Statement 1 is executed (one time)
before the execution of the code block.
• Statement 2 defines the condition for
executing the code block.
• Statement 3 is executed (every time)
after the code block has been executed.
Example
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
Example
for (int i = 0; i <= 10; i = i + 2)
{
Console.WriteLine(i);
}
The foreach Loop
• here is also a foreach loop, which is used
exclusively to loop through elements in an
array:
• Syntax
foreach (type variableName in arrayName)
{
// code block to be executed
}
Example
string[] cars = {"Volvo", "BMW", "Ford",
"Mazda"};
foreach (string i in cars)
{
Console.WriteLine(i);
}
C# Break and Continue
• C#Break
• It was used to "jump out" of a switch
statement.
• The break statement can also be used
to jump out of a loop.
Example
This example jumps out of the loop when i is equal to 4:
for (int i = 0; i < 10; i++)
{
if (i == 4)
{
break;
}
Console.WriteLine(i);
}
C# Continue
• The continue statement breaks one
iteration (in the loop), if a specified
condition occurs, and continues with the
next iteration in the loop.
This example skips the value of 4:
for (int i = 0; i < 10; i++)
{
if (i == 4)
{
continue;
}
Console.WriteLine(i);
}
Break and Continue in
While Loop
Break Example
int i = 0;
while (i < 10)
{
Console.WriteLine(i);
i++;
if (i == 4)
{
break;
}
}
Continue Example
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.
• To declare an array, define the variable
type with square brackets:
• string[] cars;
• We have now declared a variable that holds an
array of strings.
• To insert values to it, we can use an array
literal - place the values in a comma-separated
list, inside curly braces:
• string[] cars = {"Volvo", "BMW", "Ford",
"Mazda"};
• To create an aray of rintegers, you could write:
int[] myNum = {10, 20, 30, 40};
Access the Elements of an Array
• You access an array element by referring to the
index number.
• This statement accesses the value of the first
element in cars:
• Example
• string[] cars = {"Volvo", "BMW", "Ford",
"Mazda"};
• Console.WriteLine(cars[0]);
• // Outputs Volvo
Exercises
• Write a separate program for each of
the following sample output: Use for
loop.
12345 1 12345
1 2 3 4 5 1 2 1234
1 2 3 4 5 1 23 123
1 2 3 4 5 1 234 12
1 2 3 4 5 1 2345 1