0% found this document useful (0 votes)
15 views

Topic 3 - Control Structure (Part 2) - 230726 - 105926

The document discusses different types of repetition control structures in programming, including for, while, and do-while loops. It provides examples of using each loop type, such as using a for loop to print the first 3 integers or a counter-controlled while loop to find the total of input numbers. The key aspects covered are the syntax of each loop, how the flow of execution works, and examples of common uses like iterating through a range of numbers or values entered by the user. Nested loops and sentinel-controlled loops are also explained. Exercises provided apply the different loop types to problems like printing ranges of numbers or summing user inputs.

Uploaded by

Sham Iqbal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Topic 3 - Control Structure (Part 2) - 230726 - 105926

The document discusses different types of repetition control structures in programming, including for, while, and do-while loops. It provides examples of using each loop type, such as using a for loop to print the first 3 integers or a counter-controlled while loop to find the total of input numbers. The key aspects covered are the syntax of each loop, how the flow of execution works, and examples of common uses like iterating through a range of numbers or values entered by the user. Nested loops and sentinel-controlled loops are also explained. Exercises provided apply the different loop types to problems like printing ranges of numbers or summing user inputs.

Uploaded by

Sham Iqbal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

TOPIC 3 (PART II):

CONTROL STRUCTURE - REPETITION


OBJECTIVES

In this chapter you will:


 Learn about repetition (looping) control structure
 Explore how to construct and use count-controlled, and sentinel-
controlled repetition structures
 Discover how to form and use nested control structures
INTRODUCTION

 Repetition structure: Directs computer to repeat one or more


instructions until some condition is met
➢ Also called a loop or iteration

 Three basic types of loops :


➢ for
➢ while
➢ do..while
THE for LOOP

 for loop is an entry control loop when action is to be repeated for


a predetermined number of times
 The general form of the for statement is:

for (initial statement; loop condition; update statement)


{
statement1;
statement2;
statement3;
}

 The initial statement, loop condition, and update statement are


called for loop control statements
THE for LOOP (CONT.)
THE for LOOP (CONT.)

 The for loop executes as follows:


1. The initial statement executes
2. The loop condition is evaluated. If the loop condition evaluates to true
i. Execute the for loop statement.
ii. Execute the update statement (the third expression in the
parentheses).
3. Repeat Step 2 until the loop condition evaluates to false.

 The initial statement usually initializes a variable (called the for loop
control, or for indexed, variable).
 In C++, for is a reserved word.
THE for LOOP (CONT.)

 Example: This loop prints the first 3 non-negative integers

Output:
THE for LOOP (CONT.)

1 2 4
for (int i = 1; i <= 3; i++)

cout << i << “ ”;


3
i i<=3; cout << i << “ ”; i++
i=1 1<=3 1 i=1+1
(True)
i=2 2<=3 2 i=2+1
(True)
i=3 3<=3 3 i=3+1
(True)
i=4 4<=3 - -
(False)
THE for LOOP (CONT.)

 Example (cont.)

C++ code Output


THE for LOOP (CONT.)

 Example (cont.)

C++ code Output


THE for LOOP (CONT.)

 Example (cont.)

C++ code Output


EXERCISES

 Write a program segment that display all the numbers from 1 to 20.

 Write a program segment that display all the even numbers from 10 to 50.

 Write a program segment that asks the user to input 5 numbers.


THE NESTED for LOOP

 A nested loop is a loop inside the body of another loop


 The nested loop is known as the inner loop and the loop in which is
nested is known as the outer loop

Outer loop

Inner loop
THE NESTED for LOOP (CONT.)

Output:
THE NESTED for LOOP (CONT.)

 Example: Write a program to display the following output.


THE NESTED for LOOP (CONT.)

 Example: Write a program to display the following output.


THE while LOOP

 A while loop is used to repeat a specific block of code an unknown


number of times, until a condition is met
 The general form of the while statement is:

while (condition)
statement;

 while is a reserved word


 statement can be simple or compound; the body of the loop
 condition acts as a decision maker and is usually a logical expression

 The parentheses are part of the syntax


THE while LOOP (CONT.)

 condition provides an entry condition


 statement executes if the expression initially evaluates to true

 Loop condition is then re-evaluated


 Statement continues to execute until the expression is no longer true
(false)
THE while LOOP (CONT.)

 Infinite loop: continues to execute endlessly

 Can be avoided by including statements in the loop body that


assure exit condition will eventually be false
COUNTER-CONTROLLED while LOOP

 If you know exactly how many pieces of data need to be read, the while
loop becomes a counter-controlled loop
COUNTER-CONTROLLED while LOOP
(CONT.)

 Example: Find the total numbers using counter-controlled while loop

Output:
COUNTER-CONTROLLED while LOOP
(CONT.)

 Example: Find the maximum number using counter-controlled while loop


EXERCISES

 Write a program segment that display all the numbers from 1 to 20.

 Write a program segment that display all the even numbers from 10 to 50.

 Write a program segment that asks the user to input 5 numbers.


SENTINEL-CONTROLLED while LOOP

 Sentinel variables is tested in the condition and loop ends when sentinel
is countered
SENTINEL-CONTROLLED while LOOP
(CONT.)

 Example: Find the total and average of numbers using sentinel-controlled


while loop
EXERCISES

 Exercise 1: Write a segment to repeat a statement until the user enter the
sentinel value of ‘N’. The repeated statement is “Do you want to continue?
Y for Yes, N for No”

 Exercise 2: Write a program segment that asks the user to input any
numbers until it reads 999 to stop. Then display the total of the numbers.
THE do...while LOOP

 The general form of the do…while statement is:

do
statement;
while (expression);

 The statement executes first, and then the expression is evaluated


 If the expression evaluates to true, the statement executes again
 As long as the expression in a do…while statement is true, the
statement executes
THE do...while LOOP (CONT.)
THE do...while LOOP (CONT.)

 Example:

Output:
EXERCISES

 Trace the output of following C++ code:

You might also like