Comprog 3 Looping in C#
Comprog 3 Looping in C#
Comprog 3 Looping in C#
Looping
Loops
A loop refers to a construct that enables a program to execute a block of statements or a loop body repetitively as long as the
defined condition evaluates to true. Looping is used when an operation needs to repeat multiple times. The C# providesthree
(3) looping structures:
• while – This loop repeats a block of statements as long as a given condition evaluates to true. It evaluates the condition
first before executing the loop body.
• do…while – This is similar with while loop, except that it executes the block of statements before evaluating the given
condition regardless if it evaluates to true or false.
• for – This loop repeats a block of statement for a specified number of times.
The while Loop
The while loop repeats a block of statements as long as a given condition is true. The following is the general syntax of while
loop in C# including an example:
Syntax: For example:
while (condition) { //this will print a sequence of numbers from 1 to 10
//statements in loop body int start = 1;
} while (start <= 10) {
Console.WriteLine(start);
start++;
}
The condition can be a relational or logical expression that must return a true or false value. When the given condition in the
while loop evaluates to true, the loop body will execute and the condition is reevaluated. If the condition evaluates to true
again, the loop body will continue to execute until the given condition evaluates to false and the execution jumps to the
statements after the while loop.
In the given example, the initialized variable start is a loop control variable that is used to control the loop condition. The
while loop will print the numbers from 1 to 10. It is important to include statements within the loop that will update the loop
condition to false to terminate the loop execution. Otherwise, the loop body continues to execute endlessly. This is called
infinite loop. In the example, the statement start++; is used to update the condition on the while loop.
The do-while Loop
The do…while loop executes the loop body first before evaluating the given loop condition. The following is the general syntax
of do…while loop in C# including the example:
Syntax: For example:
do { //this will print a sequence of numbers from 1 to 10
//statements in loop body int start = 1;
} while (condition); do {
Console.WriteLine(start);
start++;
} while (start <= 10);
The do statement executes the loop body first, then evaluates the given condition in the while statement. If the condition
evaluates to false, the loop terminates. If it evaluates to true, then the loop body is executed again. This process repeats
until the given condition evaluates to false.
In the given example, the do statement executes the loop body first then evaluates the given condition in the whilestatement.
Similar with while loop, it is important to include statements within the loop body that will update the loop conditiontofalse
to terminate the loop execution.
Example:
for (int num = 1; num <= 10; num++) {
if (num == 5 || num == 6) {
continue; //if this statement is executed it will ignore the print statement
below and the execution jumps back to the loop
}
Console.Write(num + " ");
}
Output:
1 2 3 4 7 8 9 10
In the given example, the continue within if statement is used to skip displaying the 5 and 6.
When implementing break or continue statements, they must be within a conditional statement, such as if…else
statement.
REFERENCES:
Deitel, P. and Deitel, H. (2015). Visual C# 2012 how to program (5th Ed.). USA: Pearson Education, Inc.
Gaddis, T. (2016). Starting out with visual C# (4th Ed.). USA: Pearson Education, Inc.
Harwani, B. (2015). Learning object-oriented programming in C# 5.0. USA: Cengage Learning PTR.
03 Handout 2 *Property of STI
student.feedback@sti.edu Page 3 of 3