Download presentation
Presentation is loading. Please wait.
1
Loops (iterations, repetitions)
2
Three fundamental structures:
sequence, condition, iteration
3
Iterative Constructs: iteration or loop
A loop repeats a statement or a sequence of statements a number of times. The fundamental iteration construct: ‘while’ statement
4
The fundamental while Statement
Syntax while (bool expression) { Action } How it works: If Expression is true then execute Action Repeat this process until Expression evaluates to false Action is either a single statement or a group of statements within braces Expression true false Action
5
The first example: int n; n=5; while (n < 10) {
cout << n << endl; n=n+1; } n=5; (5<10); n=n+1; (6<10); n=n+1; (7<10); n=n+1; (8<10); n=n+1; (9<10); n=n+1; (10<10) false; finish; 10;
6
N! (while) int number, factorial, n; cout << "Enter number: ";
cin >> number; factorial = 1; n = 1; while (n <= number) { factorial = factorial*n; n=n+1; } cout << "The factorial of " << number << " is " << factorial << endl;
7
2N (while) int number, result, n; cout << "Enter number: ";
cin >> number; result = 1; n = 1; while (n <= number) { result = result*2; n=n+1; } cout << "Two raised to the " << number << " power is " << result << endl;
8
while loop summary while (condition) action initialisation(V)
while (condition(V)) action-and-update(V) Initialisation of a set of variables V Condition on V Action: V = f(V) init first action second action n=1,fac=1 fac=2,n=2 fac=6,n=3 fac=fac*n;n=n+1; fac=fac*n;n=n+1; fac=fac*n;n=n+1;
9
GCD (the greatest comon divisor) example
GCD(a,b) is the largest number that divides both a and b without leaving a remainder GCD(25,0)=? GCD(12,18) =? GCD(5,7)=? GCD(a,b)=GCD(a-b,b) if a>b GCD(a,b-a) if b>a GCD(a,b)=GCD(b,a mod b) GCD(a,0)=a Euclid method iterative
10
GCD with subtraction int a,b,gcd; cin >> a; cin >> b;
if (a==0) gcd=b; else { while (b != 0) { if (a>b) a = a-b; else b = b-a; } gcd=a; cout << “GCD is:” << gcd << endl;
11
GCD with the remainder cin >> a; cin >> b;
while (b != 0) { t = b; b = a % b; a = t; } gcd=a; cout << “GCD is:” << gcd << endl;
12
Key points of a loop: Make sure there is a statement that will eventually stop the loop (condition) Make sure to initialize loop counters correctly (initialisation) Have a clear purpose for the loop (action)
13
How to Stop a Loop Known number of iterations before the loop stops
Test for a user-controlled condition before or after each iteration
14
Common Loop Errors while(balance != 0.0) { balance = balance - amount;
} This will lead to an infinite loop! balance may not become equal zero due to numerical inaccuracies while(balance != 0.0);
15
Common Loop Errors product=? while(n <= 1000){
product = product*n; n=n+1; } sum=? while(n <= 1000) { sum = sum+n; Be sure to initialize to 0 a variable used for sums Be sure to initialize to 1 a variable used for products
16
Nested Loops Nested loops are loops within loops. They are similar in principle to nested if-else statements. Many applications require nested loops.
17
Multiplication Table (while)
// Program to output the // multiplication table int i; //Outer loop counter int j; //Inner loop counter i=1; while (i<=10) { j=1; while (j<=10) { cout << i*j << “ “; j=j+1; } cout << endl; i=i+1;
18
Diamond Pattern Print out the following diamond pattern * * * *
* * * * * * * * * * * * * * * * * * * * *
19
Diamond Pattern Subproblem: print out the upper half
print out the lower half
20
Upper triangular Pattern
Print out triangular pattern: row 1: print 4 spaces, 1 star; row 2: print 3 spaces, 3 stars; row 3: print 2 spaces, 5 stars; row 4: print 1 space, 7 stars; row 5: print 0 spaces, 9 stars; Algorithm Refinement: row 1: print (5-row) spaces, (2*row - 1) stars; row 2: print (5-row) spaces, (2*row - 1) stars; row 3: print (5-row) spaces, (2*row - 1) stars; row 4: print (5-row) spaces, (2*row - 1) stars; row 5: print (5-row) spaces, (2*row - 1) stars; * * * * * * * * * * * * * * * * * * * * * * * * *
21
Upper Triangular Pattern
int row, space, star; row=1; while(row<=5){ space=1; while(space<=5-row) { cout << " "; space=space+1; } star=1; while(star<=2*row-1) { cout << "*"; star=star+1; cout << endl ; row=row+1;
22
Diamond Pattern Subproblem: Print out lower half:
print out the upper half print out the lower half Print out lower half: Row 6 (row 4): print 1 spaces, 7 stars; Row 7 (row 3): print 2 spaces, 5 stars; Row 8 (row 2): print 3 space, 3 stars; Row 9 (row 1): print 4 spaces, 1 stars; Algorithm Refinement: row 4: print (5-row) spaces, (2*row - 1) stars; row 3: print (5-row) spaces, (2*row - 1) stars; row 2: print (5-row) spaces, (2*row - 1) stars; row 1: print (5-row) spaces, (2*row - 1) stars;
23
Diamond Pattern int row, space, star; … // top half
while(row>=1){ //bottom half space=1; while(space<=5-row) { cout << " "; space=space+1; } star=1; while(star<=2*row-1) { cout << "*"; star=star+1; cout << endl ; row=row-1;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.