Looping

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/34

flashcard set

Earn XP

Description and Tags

2Y2 | Midterms

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

35 Terms

1
New cards

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.

2
New cards

Looping

is used when an operation needs to repeat multiple times.

3
New cards

The C# provides three (3) looping structures:

4
New cards

• 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.

5
New cards

• 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.

6
New cards

• for

This loop repeats a block of statement for a specified number of times.

7
New cards

while loop

repeats a block of statements as long as a given condition is true.

8
New cards

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++;

}

9
New cards

The condition can be a relational or logical expression that must return a true or false value.

10
New cards

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.

11
New cards

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.

12
New cards

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:

13
New cards

do statement

executes the loop body first, then evaluates the given condition in the while statement.

14
New cards

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.

15
New cards

In the given example, the do statement executes the loop body first then evaluates the given condition in the while statement.

16
New cards

Similar with while loop, it is important to include statements within the loop body that will update the loop condition to false to terminate the loop execution.

17
New cards

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 expression to update the condition.

18
New cards

The 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);

}

19
New cards

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

20
New cards
  1. The initialization step is executed first, and it contains the starting value of the loop. This is executed only once.
21
New cards
  1. 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:
22
New cards

Step A. The statements in the loop body is executed.

23
New cards

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.

24
New cards

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.

25
New cards

Jump statements

are used to change the flow of control of a looping structure.

26
New cards

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.

27
New cards

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.

28
New cards

When the break statement is used in a nested loop, it only terminates the execution of the innermost loop in which it appears.

29
New cards

The following example shows how to implement a break 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

30
New cards

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

31
New cards

continue

The 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.

32
New cards

The following example shows how to implement a continue statement in a loop:

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

33
New cards

In the given example, the continue within if statement is used to skip displaying the 5 and 6.

34
New cards

When implementing break or continue statements, they must be within a conditional statement, such as if…else statement.

35
New cards

Example