Download presentation
Presentation is loading. Please wait.
Published byAlexandra Todd Modified over 8 years ago
1
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan
2
Content 2 Types of repetition statements while repetition statement Counter-controlled repetition Sentinel-controlled repetition Nested control statements for repetition statement do..while repetition statement break and continue statements Common errors
3
Repetition Statements Three types for statement and while statement Perform a group of actions zero or more times do..while statement Perform a group of actions at least once 3
4
while Repetition Statements Actions repeated while condition remains true Syntax One of the actions should causes condition to becomes false Example 4 while (condition) { action1; action2;. actionN; } int product = 3; while ( product <= 30 ) product *= 3; int product = 3; while ( product <= 30 ) product *= 3;
5
while Repetition Statements (cont.) Not providing action that causes condition to becomes false Lead to infinite loop Logic error Example 5 int product = 3; while ( product <= 30 ) cout << product; int product = 3; while ( product <= 30 ) cout << product;
6
Counter-controlled Repetition Uses a variable to control number of loops’ iterations Control counting loops with integer values Also known as definite repetition Number of repetitions known beforehand Requires Name of loop control variable Initial value of loop control variable Condition to test of reaching final value Update loop control variable 6 Initialize control var ; while (condition) { action1; action2;. actionN; update control var; }
7
7
8
Example A class of ten students took a quiz. The grades (integers in the range 0 to 100) are entered by the user. Determine the class average on the quiz. 8
9
9 Variables used to store totals are normally initialized to zero before being used in programs
10
10
11
Nested Control Statement Control statements can be one inside the another Nested building blocks Example # 1 A college has a list of test results (1 = pass, 2 = fail) for 10 students. Write a program that analyzes the results. 11
12
12
13
13 25 // if result 1, increment passes; if/else nested in while 26 if( result ==1)// if/else nested in while 27 passes = passes +1; 28 29 else// if result not 1, increment failures 30 failures = failures +1; 31 32 // incrementstudentCounterso loop eventually terminates 33 studentCounter= +1; 34 35 }// end while 36 37 // termination phase; display number of passes and failures 38 cout<<"Passed "<< passes <<endl; 39 cout<<"Failed "<< failures <<endl; 40 41 42 43 44 45 return0;// successful termination 46 47 }// end function main
14
14
15
for Repetition Statement Provide counter-controlled repetition details in a single statement Syntax for loop repeats actions until condition becomes false 15 for (initialization; loopContinuationCondition; update) action1; for (initialization; loopContinuationCondition; update) { action1; action2; … actionN; }
16
16
17
for Repetition Statement (cont.) When loop counter is declared in initialization expression, it can ONLY be used inside for statement (local variable) initialization and update expressions can be comma-separated lists of expressions 17 for(int i=0, j=0; i<4 && j<8; i++,j++) cout << “*”; for(int i=0, j=0; i<4 && j<8; i++,j++) cout << “*”;
18
for Repetition Statement (cont.) initialization, condition and update expressions are optional, but separated semicolon are required Omit initialization Initializes control variable earlier in program Omit condition Always condition is true, may lead to infinite loop Omit update Calculated as statement in for body 18
19
Examples Using for Statement Vary control variable from 100 to 1 in increments by -1 (decrement by 1) Vary control variable from 7 to 77 in steps of 7 Vary control variable over the sequence: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0 19 for(int i = 100; i >= 1; i-- ) for(int i = 7; i <= 77; i += 7 ) for(int i = 99; i >= 0; i -= 11 )
20
Examples Using for Statement (cont.) Using a comma-separated list of expressions can be written as 20 int total =0; for ( int number = 2; // initialization number <= 20; // loop continuation condition total += number, number += 2 ) // total and increment ; // empty statement int total =0; for ( int number = 2; // initialization number <= 20; // loop continuation condition total += number, number += 2 ) // total and increment ; // empty statement int total =0; for ( int number = 2; number <= 20; number += 2 ) total += number; int total =0; for ( int number = 2; number <= 20; number += 2 ) total += number;
21
Example A person invests 1000$ in a savings account yielding 5% interest. Assuming that all interest is left on deposit in the account, calculate and print the amount of money in the account at the end of each year for 10 years. Use the formula: where pis the original amount invested ris the annual rate nis the number of years ais the amount of deposit at the end of the n year 21
22
22
23
23
24
do..while Repetition Statement Similar to while statement but while statement tests loop- continuation before performing body of loop do..while tests loop-continuation after performing body of loop Loop body always executes at least once Syntax 24 do { action1; action2;. actionN; } while (condition)
25
25 What is the output produced by this loop ?? int counter=1; do { cout << counter << endl; } while (++counter <= 10); int counter=1; do { cout << counter << endl; } while (++counter <= 10);
26
break Statement Alter flow of control Causes immediate exit from control structure Used with while, for, do…while or switch statements Escape early from a loop ( while, for, do…while ) Skip the remainder of switch 26
27
27
28
continue Statement Used with while, for or do…while statements Alter flow of control Skips remainder of loop body of current iteration Proceeds with next iteration of loop With while and do…while statements Loop-continuation test is evaluated immediately after continue statement With for statement Update expression is executed Next, loop-continuation test is evaluated 28
29
29
30
Exercise - 1 30 Write a program that asks for a number and reports the summation from 1 to the number. Use while statement. Write another version of program using for statement.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.