Module 5 Repetition Control Structures Introduction to Programming





















































- Slides: 53
Module 5: Repetition Control Structures Introduction to Programming
Objectives Upon completion of this module, you will be able to: • Know the loop structure • Know the different ways on how the loop is controlled • Simulate a loop statement to determine the output
Loop Structure Condition is tested first Condition is tested later
Testing Condition First Step a false condition true Step x Step y Step n
Testing Condition First Step a condition Step x Step y Step n
Testing Condition First Step a Step x Step y true condition false Step n
Testing Condition First Step a Step x Step y true condition false Step n
How Loops are Controlled Condition Controlled Sentinel Controlled Counter Controlled • 1, 2, 3, 4, … • …, 4, 3, 2, 1
Counter Controlled Loop counter ← initial. Value test counter false value true Step x Update counter Step n
Counter Controlled Loop counter ← initial. Value test counter false value true Step x Update counter Step n
Example: Identify I/O Draw a flowchart for the following problem: Read 5 integer and display the value of their summation. Input : 5 integer n 1, n 2, n 3, n 4, n 5 Output: The summation of n 1, n 2, . . , n 5 Input example: 2 Output example: 20 3 4 5 6
Assume input example: 2 3 4 5 6 start Input n 1 Input n 2 This flowchart Input n 3 does not use loop, hence we input n 4 need to use 6 input n 5 different variables sum ← n 1+n 2+n 3+n 4+n 5 output sum end n 1 2 n 2 3 n 3 4 n 4 5 n 5 6 su m 20
Counter Controlled Loop counter ← 1, sum ← 0 counter < 6 false 1 2 3 4 5 6 count 14 20 0 14 2 5 9 er su 0 ++5 2 5 9 2 3 4 m 6 65 14<<<666 false 2 3 true input n Assume input example: 2 3 4 5 6 n 2 3 4 5 6 sum ← sum + n counter++ output sum This loop Uses only is The counter-controlled 3 variables Increases by 1
counter ← 5, sum ← 0 Decreasing Counter Controlled Loop false counter > 0 true input x sum←sum+ x counter-output sum
• Example: Draw a flowchart for this problem; • Given an exam marks as input, display the appropriate message based on the rules below: – If marks is greater than 49, display “PASS”, otherwise display “FAIL” – However, for input outside the 0 -100 range, display “WRONG INPUT” and prompt the user to input again until a valid input is entered
Assume m=110 m=57 m=5 57 5 m 110 input m “WRONG INPUT” true 57<<0<0||0||5||57 110 5 >100 110 >100 Condition 557 >> 49 controlled loop 49 with its condition being tested at the end WRONG FAIL PASS INPUT m<0 || m>100 false m>49 false “FAIL” true “PASS”
input m m<0 || m>100 false true “WRONG INPUT” input m Condition-controlled loop with its condition being tested first m>49 false “FAIL” true “PASS”
Sentinel-Controlled Loop • Draw a flowchart for a problem which: Input: A set of integers • Receive a number of positive integers ending and with a negative integer or a zero display the summation and average of these integers. • A negative or zero input indicate the end of Output: Summation and input process Average of these integers
• Input Example: • 30 16 42 • Output Example: • Sum = 88 • Average = 29. 33 -9
Try to understand What will happen if this statement is deleted? ? ? ? sum← 0 input x false x>0 true input x sum←sum+x display sum What happened if these 2 statements exchange places ?
Loop : for n n n Condition is tested first Loop is controlled by a counter Syntaxes for (initial value ; condition; update counter) statement; Or for (initial value ; condition; update counter) { statement; }
Example • Write a program which does the following: • Reads 5 integers and displays the sum of all • integers • Input example: 3 • Output example: 16 6 4 1 2
counter ← 1, sum ← 0 false counter < 6 true Recall the flowchart input x sum←sum+ x counter++ output sum
Note the initial value of i and condition i ← 1, sum ← 0 false i< 6 true input x How many times does the loop get executed? sum←sum+ x i++ output sum
i ← 0, sum ← 0 false i< 6 true input x How many times does the loop get executed? sum←sum+ x i++ output sum
i ← 0, sum ← 0 false i< 5 true input x How many times does the loop get executed? sum←sum+ x i++ output sum
The C++ statements: int x, sum, i; sum = 0; for (i = 0; i < 5; i++) { cin>>x; sum = sum + x; } cout<<sum;
i ← 0, sum ← 0 false i< 5 true input x sum←sum+ x i++ output sum int x, sum, i; sum = 0; for (i = 0; i < 5; i++) { cin>>x; sum = sum + x; } cout<<sum;
for statement • Example: • for ( num = 1; num <= 3; num++ ) • cout<<num; 1 _ _ Introduction to Computer Programming ? ? ? num
for statement • Example: • for (num = 1; num <= 3; num++ ) • cout<<num<<“ “; _ Introduction to Computer Programming 1 num
for statement • Example: • for (num = 1; num <= 3; num++ ) • cout<<num<<“ “; _ 1 num
for statement • Example: • for (num = 1; num <= 3; num++ ) • cout<<num<<“ “; 1 _ 1 num
for statement • Example: • for (num = 1; num <= 3; num++ ) • cout<<num<<“ “; 1 _ 2 num
for statement • Example: • for (num = 1; num <= 3; num++ ) • cout<<num<<“ “; 1 _ 2 num
for statement • Example: • for (num = 1; num <= 3; num++ ) • cout<<num<<“ “; 1 2 _ 2 bil
for statement • Example: • for (num = 1; num <= 3; num++ ) • cout<<num<<“ “; 1 2 _ 3 num
for statement 3 num • Example: • for (num = 1; num <= 3; num++ ) • cout<<num<<“ “; 1 2 _ Introduction to Computer Programming
for statement 3 num • Example: • for (num = 1; num <= 3; num++ ) • cout<<num<<“ “; 1 2 3 _ Introduction to Computer Programming
for statement 4 num • Example: • for (num = 1; num <= 3; num++ ) • cout<<num<<“ “; 1 2 3 _ Introduction to Computer Programming
for statement 4 num • Example: • for (num = 1; num <= 3; num++ ) • cout<<num<<“ “; 1 2 3 _ Introduction to Computer Programming
for while do-while
Loop: while § Condition is tested first § Loop is controlled by condition or a counter § Syntax while (condition) statement; Or while (condition) { statement; }
Recall this example: Given an exam marks as input, display the appropriate message based on the rules below: q If marks is greater than 49, display “PASS”, otherwise display “FAIL” q However, for input outside the 0 -100 range, display “WRONG INPUT” and prompt the user to input again until a valid input is entered
input m false m<0 || m>100 true “WRONG INPUT” input m Exercise: Convert this flowchart to a C++ program m>49 false “FAIL” true “PASS”
int marks; cin>>marks; while (marks<0) | | (marks>100) { cout<<“WRONG INPUT”; cin>>marks; } if (marks>49) { cout<<“PASS”; else cout<<“FAIL”; Double Selection
Exercise • Given a set of integers with the last one being 999 Draw the • Display the summation of all the integers. flowchart for this problem • Input example: • 1 3 23 999 • Output example: • Sum = 27
Sentinel-controlled loop #include <iostream> using namespace std; sum=0 void main() { input x int sum, x; sum = 0; false cin>>x; x!=999 while (x != 999) { true sum = sum + x; sum←sum+x cin>>x; } input x output sum cout<<“The sum : “<<sum; }
int sum, x; sum = 0; cin>>x; while (x != 999) { 123 3999 != != sum + x; 999 999 cin>>x; } cout<<“The sum : “<<sum; _ 1 3 The sum : 27 23 999 x 999 23 1 3 ? su m 4+23 0+1 1+3 27 0 1 4 ?
Do-while Loop • Statements in the loop are executed first (at least once, and condition is tested last • Loop is controlled by a condition or counter • Syntax • do { • statement; • } while (condition); • statement;
? ? ? 65 66 67 68 start • Example : • cout<<“Input start and end value : “; • cin>>start; cin>>end; • do { • cout<< start; • start++; 66 <= 67 67 • } while (start <= end) ; 68 67 _ Input start and end value : 65 _ 67 67_ 65 _ 66 _ 67 _ ? ? ? 67 end
anevennumber. Printififeven!!! ! _ number. _ is isisan an an even number. Print even 0000 is Print continue statement number. Print ifif even ! is isan an anevennumber. _ 222_is an even number. _ number. Print if even ! _isis an 44 Example: for ( i = 0; i <= 5; i++ ) { if ( i % 2 ) continue; else cout<<i<<“ is an even number. ”; cout<<“Print if even ! n”; } i i <= 5 i%2 0 1 5 6 4 3 2 64 05<= 1 2 3 <= <=55 95 false true 0 1
Input a value between 17: 5 4 _ break statement 1 2 3 Yee. Pee! I’m out of the 3 cout<<“Input a value between loop!1 – 7: ”; ? ? ? 4 cin>>value; for (i = 1; i <= 7; i++) { if ( i = = value ) break; cin>>endl>>i; i i <= 1 43 2 3 4 21<= <= <= } 7777 true cout<<“Yee. Pee! I’m out of the loop!n”; valu e i == 24 3 1== ==44 true value false
Exercises • Create a program that will count from 1 to n, wherein n is a user-input number. • Create a menu and combine previously-created programs into one. Follow the format below: – Option 1 : Program 1 – Option 2 : Program 2 – Option 3 : Exit The program must return to the main menu unless Option 3 was selected.