CHAPTER 4 REPETITION CONTROL STRUCTURE CSC 126Fundamental of

  • Slides: 36
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 (while statement) • for statement • Basic iterative problems (for statement) • Nested loops

FOR LOOP

FOR LOOP

INTRODUCTION • Also called as a counted or indexed for loop • The general

INTRODUCTION • Also called as a counted or indexed for loop • The general form of the for statement is: for ([initial statement]; loop condition; [update statement]) statement; • The initial statement, loop condition, and update statement are called for loop control statements • Items in square brackets ([ ]) are optional.

INTRODUCTION for ([initial statement]; loop condition; [update statement]) statement; • The for loop executes

INTRODUCTION for ([initial statement]; loop condition; [update statement]) statement; • The for loop executes as follows: • 1. The initial statement executes. • 2. The loop condition is evaluated. If the loop condition evaluates to true • ii. parentheses). Execute the for loop statement. Execute the update statement (the third expression in the • Repeat Step 2 until the loop condition evaluates to false. • The initial statement usually initializes a variable. • In C++, for is a reserved word.

INTRODUCTION • Example : Displaying numbers 1 through 3 initialization condition update for (int

INTRODUCTION • Example : Displaying numbers 1 through 3 initialization condition update for (int count = 1; count <= 3; count = count + 1) cout << count << endl; Result: 1 2 3

FOR LOOP – EXAMPLE 1 • Using for loop to display ‘Welcome to C++’.

FOR LOOP – EXAMPLE 1 • Using for loop to display ‘Welcome to C++’. • Pseudocode: Start For( set i to 1; i less than or equal to 3; add 1 to i) display “welcome to C++” Endfor End

FOR LOOP – EXAMPLE 1 Start • Flowchart i = 1 F i <=

FOR LOOP – EXAMPLE 1 Start • Flowchart i = 1 F i <= 3 T Display “welcome to C++” i ++ End

for LOOP – EXAMPLE 2 • Example: to create a program to display backward

for LOOP – EXAMPLE 2 • Example: to create a program to display backward the first 10 non negative number.

for LOOP – EXERCISES • Exercise 1: create a program that display the first

for LOOP – EXERCISES • Exercise 1: create a program that display the first 10 positive odd integers.

for LOOP – EXERCISES • Exercise 1 - answer

for LOOP – EXERCISES • Exercise 1 - answer

for LOOP – EXERCISES • Exercise 2: how many time the following loop processed?

for LOOP – EXERCISES • Exercise 2: how many time the following loop processed? for (int count = 6; count < 6; count = count + 1) cout << count << endl; • Answer:

for LOOP – EXERCISES • Exercise 3: how many time the following loop processed?

for LOOP – EXERCISES • Exercise 3: how many time the following loop processed? for (int count = 4; count <= 10; count = count + 2) cout << count << endl; • Answer:

for LOOP – EXERCISES JAN 2018

for LOOP – EXERCISES JAN 2018

FOR LOOP – EXERCISES MAR 2016

FOR LOOP – EXERCISES MAR 2016

for LOOP A semicolon at the end of the for statement (just before the

for LOOP A semicolon at the end of the for statement (just before the body of the loop) is a semantic error. In this case, the action of the for loop is empty. In the for statement, if the loop condition is omitted, it is assumed to be true. In a for statement, you can omit all three statements—initial statement, loop condition, and update statement. The following is a legal for loop:

FOR LOOP for (; ; ) cout << "Hello" << endl; • This is

FOR LOOP for (; ; ) cout << "Hello" << endl; • This is an infinite loop, continuously printing the word Hello

BASIC ALGORITHM USING for LOOP STRUCTURE • Calculate total/ sum • Calculate average •

BASIC ALGORITHM USING for LOOP STRUCTURE • Calculate total/ sum • Calculate average • Find maximum or minimum value • count

BASIC ALGORITHM USING for LOOP STRUCTURE • Calculate sum or total: Task: To calculate

BASIC ALGORITHM USING for LOOP STRUCTURE • Calculate sum or total: Task: To calculate total of 5 numbers total = 0; *Accumulator used to calculate total for(i = 1; i <= 5; i++){ cin>>number; total = total + number; } cout<<“ The total is : ”<<total;

for LOOP – EXERCISES 1 (CALCULATE TOTAL) v Exercise 4: Suppose j, sum, and

for LOOP – EXERCISES 1 (CALCULATE TOTAL) v Exercise 4: Suppose j, sum, and num are int variables, and the input values are 26, 34, 61, 4, and -1. What is the output of the code below? Produce tracing table. cout << "Enter a number : "; cin >> num; for (j = 1; j <= 4; j++) { sum = sum + num; cout << "Enter a number : "; cin >> num; } cout << sum << endl;

for LOOP – EXERCISES 1 (CALCULATE TOTAL) v Exercise 4: answer

for LOOP – EXERCISES 1 (CALCULATE TOTAL) v Exercise 4: answer

for LOOP – EXERCISES 2 (CALCULATE TOTAL) MAR 2016

for LOOP – EXERCISES 2 (CALCULATE TOTAL) MAR 2016

BASIC ALGORITHM USING for LOOP STRUCTURE • Calculate average: Task: To calculate average of

BASIC ALGORITHM USING for LOOP STRUCTURE • Calculate average: Task: To calculate average of 5 numbers total = 0; average = 0; for(i = 1; i <= 5; i++){ cin>>number; total = total + number; } average = total/ (i -1); cout<<“ The average is : ”<<average;

BASIC ALGORITHM USING for LOOP STRUCTURE • To find maximum value: Task: To find

BASIC ALGORITHM USING for LOOP STRUCTURE • 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 cin>>number; max = number; max = -1; for(i = 2; i <= 5; i++) { cin>>number; if(number>max) max = number; } cout<<“ The maximum value is : ”<<max; for(i = 1; i <= 5; i++) { cin>>number; if(number>max) max = number; } cout<<“ The maximum value is : ”<<max;

BASIC ALGORITHM USING for LOOP STRUCTURE • To find minimum value: Task: To find

BASIC ALGORITHM USING for LOOP STRUCTURE • 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; min = 1000; for(i = 2; i <= 5; i++) { cin>>number; if(number<min) min = number; } cout<<“ The minimum value is : ”<<min; for(i = 1; i <= 5; i++) { cin>>number; if(number<min) min = number; } cout<<“ The minimum value is : ”<<min;

for LOOP – EXERCISES 1 (MAX) MAR 2017

for LOOP – EXERCISES 1 (MAX) MAR 2017

BASIC ALGORITHM USING REPETITION STRUCTURE • Count Task: user requires to input 5 numbers.

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

for LOOP – EXERCISES 1 (COUNT) Write a complete C++ program to allow user

for LOOP – EXERCISES 1 (COUNT) Write a complete C++ program to allow user to input 10 numbers using for loop. Count and display how many numbers that can be divisible by 2 and by 5.

NESTED LOOP

NESTED LOOP

NESTED LOOP • In many situations, it is very convenient to have a loop

NESTED LOOP • In many situations, it is very convenient to have a loop contained within another loop. • Such loops are called nested loops. • For each single trip, through the outer loop, the inner loop runs through its entire sequence. • Each time counter i increases by 1, the inner loop executes completely.

NESTED LOOP

NESTED LOOP

NESTED LOOP • Example of C++ program segment for (i = 0; i <=

NESTED LOOP • Example of C++ program segment for (i = 0; i <= 5; i++) { cout << "n i is now " << i << endl; for (j = 1; j <= 4; j++) cout << " j = " << j ; }

NESTED LOOP • How it works… Outer loop i is now 0 j =

NESTED LOOP • How it works… Outer loop i is now 0 j = 1 j = 2 j = 3 j = 4 i is now 2 j = 1 j = 2 j = 3 j = 4 i is now 3 j = 1 j = 2 j = 3 j = 4 i is now 4 j = 1 j = 2 j = 3 j = 4 i is now 5 j = 1 j = 2 j = 3 j = 4 Inner loop

NESTED LOOP - EXERCISE MAR 2016

NESTED LOOP - EXERCISE MAR 2016

NESTED LOOP - EXERCISE APR 2009

NESTED LOOP - EXERCISE APR 2009

THE END

THE END