680 likes | 890 Views
Control Statements. CS 141 Computer Programming 1. Teacher Assistant AlAnoud Bahomaid. Outline. Control structure in c++ Repetition Structure Counter control loop While repetition structure For repetition structure Optional expressions in the for statement header Common logical error
E N D
Control Statements CS 141Computer Programming 1 Teacher Assistant AlAnoudBahomaid
Outline Control structure in c++ Repetition Structure Counter control loop While repetition structure For repetition structure Optional expressions in the for statement header Common logical error The counter variable Scope For repetition example Convert for and while Do… while repetition structure Nesting loop Nested control structure Continue Break
repetition structure Definition • It’s a control structure that repeats a group of steps in a program • Action repeated while some condition remains true • while • for • do.. while
repetition structure When use repetition structure • ask yourself some of the following questions to determine whether loops will be required in your code: • Were there any steps I repeated as I solved the problem? If so, which ones? • If the answer to question 1 is yes, did I know in advance how many times to repeat the steps? • If the answer to question 2 is no, how did I knowhow long to keep repeating the steps?
counter-controlled loop Definition • a loop whose required number of iterations can be determined before loop execution begins Essentials of counter-controlled repetition requires: The name of a control variable (loop counter) The initial value of the control variable. The loop-continuation condition that test for the final value of the control variable. The increment (or decrement) by which the control variable is modified each time through the loop
Sentinel-controlled loop Definition • A loop whose number of iterations is UNKNOWN. (indefinite repetition). • Use a special value called sentinel value (signal value, dummy value or a flag value). • Indicates the end of the loop.
1-While Repetition Structure (1-10) Activity Diagram True Action (a) Condition False
1-While Repetition Structure (2-10) • If the controlling expression( condition) is true, the loop body is then executed before the controlling expression is evaluated once more. • If the controlling expression (condition) is false, i.e. expression evaluates to false, the program goes on to execute the statement following the while loop. • If the body of the counter-controlled repetition contains more than one statement, you should surround its body by braces { }.
1-While Repetition Structure (2-10) Syntax No semicolon after while statement While( loop repetition condition) statement ; OR While( loop repetition condition) { statements ; . . }
1-While Repetition Structure (4-10) Question Write a program that print numbers from 1 to 10 using (while) loop
1-While Repetition Structure (7-10) • In previous Example: • Control variable name : counter • Initial value : 1 • The loop continuation condition: counter<=10 • The loop counter is incremented : ++counter ;
1-While Repetition Structure (8-10) Question Write a program to find the average of the students grade for ten subjects in the class.
2- for Repetition Structure (1-8) Syntax • for ( initialization ;LoopContinuationTest; increment ) • statement OR for ( initialization ;LoopContinuationTest; increment ) { statementS . . } No semicolon after for statement
(2- for Repetition Structure (2-8 As we said in while, If you need to repeat more than one statement in a program loop, you must place the statements in a block marked by braces { }.
2- for Repetition Structure (3-8) Question Write a program that print numbers from 1 to 10 using (for) loop
2-for Repetition Structure(7-8) • Initialization and increment: • For multiple variables, use comma-separated lists for (inti = 0, j = 0; j + i <= 10; j++,i++ ) cout << j + i << endl;
2-for Repetition Structure (8-8) • Vary control variable from 1 to 5 in increments of 1 • for ( inti = 1; i <= 5; i++ ) • Vary control variable from 5 to 1 in decrements of 1 • for( inti = 5; i >= 1; i-- ) • Vary control variable from 7 to 77 in steps of 7 • for( inti = 7; i <= 77; i += 7 ) • Vary control variable from 20 to 2 in steps of -2 • for( inti = 20; i >= 2; i -= 2 ) • Vary control variable over the sequence: 2, 5, 8, 11, 14, 17, 20 • for( inti = 2; i <= 20; i += 3 ) • Vary control variable over the sequence: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0 • for( inti = 99; i >= 0; i -= 11 )
Optional expressions in the for statement header • All the three expressions in the for statement header are optional . • The two semicolon are required. • Omitting the loopContinuationCondition: • C++ assumes that the condition is true. for ( inti=0 ; ; i++) • Omitting the initialization expression: • The counter variable must be initialized earlier in the program. Inti=0; for( ; i< 10 ; i++) • Omitting increment expression: • The increment is calculated by statement in the body. for ( inti=0 ; i<10 ; ) i++;
Loop Loop Loop loop . . 10. loop Optional expressions in the for statement header Examples: 0123456789 Infinite loop - initialize control variable before the loop - increment control variable in the loop body
Common logical Error • Be sure to verify that a loop’s repetition condition will become false (0); otherwise, an infinite loop may result. This condition will never be false Example • for (int counter=1 ; counter <= 25 ; counter--) • cout << counter << endl; intcounter = 1; while (counter <= 25){ cout << counter << endl; counter -- ; } This condition will never be false
The counter variable Scope If the initialization expression declares the control variable , the control variable can be used only in the body of the for statements. This is what we called variable scope.
for Repetition Example (1-5) Question Write a program that calculate the sum of evenintegers from 2 through 100 . then determine the scope of the control variable .
for Repetition Example (5-5) Thescope of the control variable (number) is: Line 14 and Line 15
Convert for and while • for loops can usually be rewritten as while loops for(initialization; loopContinuationCondition; increment) { Statement } Is Equivlent to: initialization; while (loopContinuationCondition) { statement increment; }
3-do… while structure Repetition (1-9) • The do…while repetition statement is similar to the while statement. • In the while: • The loop continuation condition test occurs at the beginningof the loop before the body of the loop executes. • In the do … while: • The loop continuation condition test occurs after the loop body executes. • The loop body always executes at least once. • Recommended to use braces in the do.. While to avoid confusing with while statements.
3-do… while structure Repetition (2-9) Activity Diagram Action (a) True Condition False
3-do… while structure Repetition (3-9) Syntax Don’t forget semicolon after condition do { statement } while ( condition );
3-do… while structure Repetition (4-9) Question Write a program that print numbers from 1 to 10 using (do.. while) loop
Notice the pre increment in loop-continuation condition. 3-do… while structure Repetition (7-9) Another solution
3-do… while structure Repetition (8-9) Question Enter a number (less than 1 = quit): 3 Enter a number (less than 1 = quit): 1 Enter a number (less than 1 = quit): 4 Enter a number (less than 1 = quit): 0 The average is: 2 Press any key to continue . . . Enter a number (less than 1 = quit): 0 No average Press any key to continue . . . Write a program that request the user to type positive numbers (one each time), and calculate the average when the user enter a number less than one.
Nesting Loops (1-7) Loops can be nested, that is, the loop body can also contain a loop.
Nesting Loops (2-7) Question • Write a program that print this using Loop
Nesting Loops (5-7) Question • Write a program that print this using Loop