C# Looping Structures: while, do-while, for, break, and continue, Study Guides, Projects, Research of Computer Science

An overview of looping structures in c# programming language, including while, do-while, for loops, and the use of break and continue statements. It explains how each looping structure works, their syntax, and examples.

Typology: Study Guides, Projects, Research

2021/2022

Uploaded on 10/31/2022

alexis-gacelo
alexis-gacelo 🇵🇭

4

(1)

5 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
IT1907
03 Handout 2 *Pr operty of STI
student.feedback@sti.edu Page 1 of 3
Looping
Loops
A loop ref ers to a construct that enable s a program to exe cute a block of stateme nts or a loop body repetitively as long as the
define d condition evaluates to true. Looping i s used whe n an operation needs to repeat multi ple ti mes. The C# pr ovides three
(3) looping structures:
whileThis loop repeats a block of statements as l ong as a given condition evaluates to true. It evaluates the condition
first before executing the loop body.
do…whileThis is similar with while l oop, exce pt that it exe cutes the bl ock of statements be fore eval uating the given
condition regardless if it evaluates to true or false.
forThis 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 fol lowing is the ge neral syntax of while
loop in C# including an example :
Syntax:
while (condition) {
//statements in loop body
}
For example:
//this will print a sequence of numbers from 1 to 10
int start = 1;
while (start <= 10) {
Console.WriteLine(start);
start++;
}
The conditio n can be a relational or logical expression that must return a true or false value. When the given condition in th e
while loop evaluates to true, the l oop body will execute and the conditi on 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 i mportant to include statements within the loop that will update the loop
condition to false to termi nate the loop exe cution. Otherwi se , the loop body continues to exe cute 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 conditi on. The following is the general syntax
of do…while loop in C# including the example:
Syntax:
do {
//statements in loop body
} while (condition);
For example:
//this will print a sequence of numbers from 1 to 10
int start = 1;
do {
Console.WriteLine(start);
start++;
} while (start <= 10);
The do statement exe cutes the loop body first, then evaluates the given condition in the while statement. If the condition
eval uates to false, the loop terminates. If it eval uates to true, then the loop body is executed again. This process repeats
until the given condition evaluates to false.
In the given e xample, the do statement ex ecutes the loop body fi rst then evaluates the given condition in the while statement.
Similar with while loop, it is important to include statements withi n the loop body that will update the loop condition to false
to terminate the loop execution.
pf3

Partial preview of the text

Download C# Looping Structures: while, do-while, for, break, and continue and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

03 Handout 2 _Property of STI_*

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: while ( condition ) { //statements in loop body }

For example: //this will print a sequence of numbers from 1 to 10 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: do { //statements in loop body } while ( condition );

For example: //this will print a sequence of numbers from 1 to 10 int start = 1; 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.

03 Handout 2 _Property of STI_*

The for Loop

The for loop executes a block of statements for a specific number of times. This looping structure specifies the elements of counter-controlled-repetition in a single line which includes these steps: initialization , condition , and the expressiontoupdate the condition. The for loop in C# takes the following general syntax including an example: Syntax: for ( initialization ; condition ; update ) { //statements in loop body }

For example: //this will print the numbers from 1 to 10 for (int start = 1; start <= 10; start++) { Console.WriteLine(start); }

The following is the flow of control of a for loop:

  1. The initialization step is executed first, and it contains the starting value of the loop. This is executed only once.
  2. The defined condition is then evaluated. If it evaluates to false, then the loop terminates and the loop body will not be executed. However, if the condition evaluates to true: Step A. The statements in the loop body is executed. Step B. After executing the loop body, the flow of control jumps up to the update step to update the loop control variable or condition. Step C. The condition is reevaluated. If it evaluates to true, then repeat Step A. However, if it evaluates to false, then the loop execution is terminated.

The break and continue Statements

Jump statements are used to change the flow of control of a looping structure. The break and continue statements are jump statements in C#, and both are keywords. These are used to terminate a loop or skip some statements within the loop.

The break terminates a loop or a switch statement and transfers the flow of program execution to the statements following the enclosing loop or switch statement. When the break statement is used in a nested loop, it only terminates the execution of the innermost loop in which it appears. The following example shows how to implement a break statement in a loop:

In the given example, the for loop will be terminated as soon as the value of variable num equals to 5.

The continue statement is used to skip the remaining statements in the loop body and immediately reevaluates the condition if it’s a while or do…while loop, or it jumps to the update step if it’s a for loop. The following example shows how to implement a continue statement in a loop:

Example: for (int num = 1; num <= 10; num++) { if (num == 5) { break; //if this statement is executed the loop will stop } Console.Write(num + " "); } Output: 1 2 3 4