CHAPTER 4 REPETITION CONTROL STRUCTURE CSC 126Fundamental of

  • Slides: 43
Download presentation
CHAPTER 4: REPETITION CONTROL STRUCTURE CSC 126|Fundamental of Algorithms & Computer Problem Solving

CHAPTER 4: REPETITION CONTROL STRUCTURE CSC 126|Fundamental of Algorithms & Computer Problem Solving

TABLE OF CONTENT • Introduction • Requirement of repetition structure • while statement •

TABLE OF CONTENT • Introduction • Requirement of repetition structure • while statement • Basic iterative problems • for statement • Nested loops

INTRODUCTION

INTRODUCTION

INTRODUCTION • It is used when a statement or a block of statements need

INTRODUCTION • It is used when a statement or a block of statements need to be executed several times. • Programmers use the repetition structures, referred to more simply as a loop, when they need the computer to repeatedly process one or more program instructions until some condition is met, at which time the repetition structures end. • Repetition is also known as iteration or loop. Write I LOVE YOU 100 times

ITERATION CONTROL STRUCTURE Write an algorithm that will ask user to input 5 numbers,

ITERATION CONTROL STRUCTURE Write an algorithm that will ask user to input 5 numbers, then calculate the average of it. Output the average value. Start initialize sum with 0 display “Enter 5 number : “ read no 1, no 2, no 3, no 4, no 5 sum = no 1+no 2+no 3+no 4+no 5 average = sum / 5 display “The total is“, average End What will happen if the algorithm need the user to repeat inserting more then 5 numbers? 10 numbers ? 100 numbers? 1000 numbers?

REQUIREMENT OF REPETITION STRUCTURE • Execution of the loop body is controlled by 3

REQUIREMENT OF REPETITION STRUCTURE • Execution of the loop body is controlled by 3 operation: • Initialization • Evaluation/ condition/ end value • Update

REQUIREMENT OF REPETITION STRUCTURE: INITIALIZATION • Process of assigning value to a loop control

REQUIREMENT OF REPETITION STRUCTURE: INITIALIZATION • Process of assigning value to a loop control variable. • Example of statement: counter = 0 number = 2 Loop Control Variable (LCV)

REQUIREMENT OF REPETITION STRUCTURE: EVALUATION/ CONDITION/ END VALUE • It controls the number of

REQUIREMENT OF REPETITION STRUCTURE: EVALUATION/ CONDITION/ END VALUE • It controls the number of times the statement or the block of statements is being executed. • Examples: counter <=10 status != ‘N’ number != 999

REQUIREMENT OF REPETITION STRUCTURE: UPDATE • Is done by adding/ subtracting a constant, such

REQUIREMENT OF REPETITION STRUCTURE: UPDATE • Is done by adding/ subtracting a constant, such as 1 or 2, to the value of a variable. • Examples: counter = counter + 1 counter ++ counter = counter - 1 counter -- number = number – 2

REQUIREMENT OF REPETITION STRUCTURE Pseudocode Depend on type of loop structure use (while or

REQUIREMENT OF REPETITION STRUCTURE Pseudocode Depend on type of loop structure use (while or for) Initialize LCV Begin counter = 1 Repeat (counter <= 5) output “I love C++” counter ++ End evaluate LCV update LCV Variable counter is known as LCV

REQUIREMENT OF REPETITION STRUCTURE Flow Chart Loop body Start Initialize LCV Counter = 1

REQUIREMENT OF REPETITION STRUCTURE Flow Chart Loop body Start Initialize LCV Counter = 1 Counter <= 5 F Evaluate LCV (loop condition) T I love C++ Counter ++ End Update LCV

while LOOP • The pseudocode for while…end. While statement for loop: Initial value of

while LOOP • The pseudocode for while…end. While statement for loop: Initial value of a loop (initialization) while (condition) statement(s) to be repeated statement to update the condition (update statement) end. While

while LOOP • The flowchart for while…end. While statement for loop:

while LOOP • The flowchart for while…end. While statement for loop:

while LOOP • There are two types of repetition can be implemented. • Counter

while LOOP • There are two types of repetition can be implemented. • Counter Loop • where the number of repetition is identified • Sentinel Loop • Where the number of repetition is unknown The while loop can be used to implement both type of repetition control structure.

WHILE LOOP (COUNTER CONTROL)

WHILE LOOP (COUNTER CONTROL)

while LOOP • General syntax: LCV = initial_value; //initialize the LCV while(LCV <= final_value)//expression

while LOOP • General syntax: LCV = initial_value; //initialize the LCV while(LCV <= final_value)//expression test the LCV { statement_to_be_executed; : LCV = LCV + step; //update the LCV }

while LOOP LCV = initial_value; while(LCV <= final_value) { statement_to_be_executed; : LCV = LCV

while LOOP LCV = initial_value; while(LCV <= final_value) { statement_to_be_executed; : LCV = LCV + step; } • Loop condition acts as a decision maker and is usually a logical expression. • A Boolean expression that controls the execution of the body of the loop • statement executes if the condition initially evaluates to true • loop condition is then reevaluated • statement continues to execute until the condition is no longer true

while LOOP • Consider: int count = 0; //initialize the LCV while (count <

while LOOP • Consider: int count = 0; //initialize the LCV while (count < 3) //test the LCV { cout << “Hi” << endl; //Loop Body count = count + 1; //Update LCV } • Loop body executes how many times? count = 0 count < 3 Output count (count = count + 1) 0 0 < 3 (T) Hi 1 1 < 3 (T) Hi 2 2 < 3 (T) Hi 3 3 < 3 (F) exit So, loop body executes 3 times

EXERCISES MAR 2017

EXERCISES MAR 2017

BASIC ITERATIVE PROBLEMS • Summation • Average • Counting • Minimum • Maximum

BASIC ITERATIVE PROBLEMS • Summation • Average • Counting • Minimum • Maximum

SUMMATION • Calculate sum or total: Task: To calculate total of 5 numbers total

SUMMATION • Calculate sum or total: Task: To calculate total of 5 numbers total = 0; i = 1; while(i <= 5){ cin>>number; total = total + number; i++; } cout<<“ The total is : ”<<total; • Accumulator – to find totals. sum = sum + variable • Is done by adding a variable to another variable. *Accumulator

SUMMATION total = 0; i = 1; while(i <= 5){ cin>>number; total = total

SUMMATION total = 0; i = 1; while(i <= 5){ cin>>number; total = total + number; i++; } cout<<“ The total is : ”<<total; Sample data: 10 5 4 6 8 Produce tracing table for the above sample data total = 0 i=1 i<=5 cin>>number total=total+number i++ 0 1 T 10 10=0+10 2 T 5 15=10+5 3 T 4 19=15+4 4 T 6 25=19+6 5 T 8 33=25+8 6 F - - - cout<<total 33

SUMMATION - EXERCISE APR 2009

SUMMATION - EXERCISE APR 2009

AVERAGE • Calculate average: Task: To calculate average of 5 numbers total = 0;

AVERAGE • Calculate average: Task: To calculate average of 5 numbers total = 0; average = 0; i = 1; while(i <= 5){ cin>>number; total = total + number; i++; } average = total/i; cout<<“ The average is : ”<<average;

AVERAGE total = 0; average = 0; i = 1; while(i <= 5){ cin>>number;

AVERAGE total = 0; average = 0; i = 1; while(i <= 5){ cin>>number; total = total + number; i++; } Sample data: 10 25 4 6 30 Produce tracing table for the above sample data average = total/(i-1); cout<<“ The average is : ”<<average; Depends on how u initialize your LCV total = 0 i=1 i<=5 cin>>number total=total+number i++ average=total /(i-1) cout<<avera ge

AVERAGE – EXERCISE APR 2009

AVERAGE – EXERCISE APR 2009

MAXIMUM • To find maximum value: Task: To find maximum value of 5 numbers

MAXIMUM • To find maximum value: Task: To find maximum value of 5 numbers *count start at 2 because first number had been entered before the loop Style 1 cin>>number; max = number; i=2 while( i <= 5) { cin>>number; if(number>max) max = number; i++; } cout<<“ The maximum value is : ”<<max; Assume -1 is the maximum value Style 2 max = -1; i=1 while( i <= 5) { cin>>number; if(number>max) max = number; i++; } cout<<“ The maximum value is : ”<<max;

MAXIMUM cin>>number; max = number; i=2 while( i <= 5) { cin>>number; if(number>max) max

MAXIMUM cin>>number; max = number; i=2 while( i <= 5) { cin>>number; if(number>max) max = number; i++; } cout<<“ The maximum value is : ”<<max; Sample data: 10 50 4 6 30 Produce tracing table for the above sample data

MAXIMUM max = -1; i=1 while( i <= 5) { cin>>number; if(number>max) max =

MAXIMUM max = -1; i=1 while( i <= 5) { cin>>number; if(number>max) max = number; i++; } cout<<“ The maximum value is : ”<<max; Sample data: 25 4 6 30 10 Produce tracing table for the above sample data

MINIMUM • To find minimum value: Task: To find minimum value of 5 numbers

MINIMUM • To find minimum value: Task: To find minimum value of 5 numbers *count start at 2 because first number had been entered before the loop cin>>number; min = number; i = 2; while( i <= 5) { cin>>number; if(number<min) min = number; i++; } cout<<“ The minimum value is : ”<<min; Assume current minimum value 1000 min = 1000; i = 1; while( i <= 5) { cin>>number; if(number<min) min = number; i++; } cout<<“ The minimum value is : ”<<min;

MINIMUM - EXERCISE OCT 2012

MINIMUM - EXERCISE OCT 2012

COUNTING • Count Task: user requires to input 5 numbers. Calculate and entered. display

COUNTING • Count Task: user requires to input 5 numbers. Calculate and entered. display how many even numbers in a group of 5 numbers *counter used to count amount of even number count. E = 0; i=1 while( i <= 5) { cin>>number; if(number%2==0) count. E= count. E + 1; i++; } cout<<“ The amount of even numbers are : ”<<count. E;

COUNTING - EXERCISE • Write a complete C++ program to allow user to input

COUNTING - EXERCISE • Write a complete C++ program to allow user to input 10 numbers using while loop. Count and display how many numbers that can be divisible by 3 and by 5.

WHILE LOOP – SENTINEL CONTROL

WHILE LOOP – SENTINEL CONTROL

WHILE LOOP • A sentinel-controlled while loop uses a special value called sentinel to

WHILE LOOP • A sentinel-controlled while loop uses a special value called sentinel to control the loop. • Sentinel value is a special value that indicates the end of a set of data or of a process • Sentinel variable is tested in the condition and loop ends when sentinel is encountered

WHILE LOOP • General syntax:

WHILE LOOP • General syntax:

 • Example #include <iostream> #include <conio> int main() { char answer; initialization Evaluation

• Example #include <iostream> #include <conio> int main() { char answer; initialization Evaluation cout << "Do you want to quit (Y - yes, N - no) : cin >> answer; "; } Sentinel value while (answer != 'Y') { cout << "Welcome to the program. " << endl; cout << "Do you want to quit (Y - Yes, N - No) : cin >> answer; Update statement } cout << "Bye. "; getch(); return 0; ";

WHILE LOOP • Exercise 1: to create a program to calculate and print total

WHILE LOOP • Exercise 1: to create a program to calculate and print total of a few numbers and the program will stop and display the result when a value 333 is read.

void main() { int number, total; total= 0; cout << "Enter a number :

void main() { int number, total; total= 0; cout << "Enter a number : "; cin >> number; while (number != 333) { total = total + number; cout << "Enter a number : "; cin >> number; } cout <<"You have entered 333 to terminate” <<“the program. “<<endl; cout <<“the total is “<<total; } getch(); Sample data: 10 25 4 6 333 Produce tracing table for the above sample data

EXERCISE OCT 2016

EXERCISE OCT 2016

EXERCISE MAR 2013

EXERCISE MAR 2013

THE END

THE END