Control Structures of C++ Programming (2) CISC1600/1610 Computer Science I/Lab Fall 2016 CISC 1600 Yanjun Li 1 Loops Purpose: Execute a block of code multiple times (repeat) Types: for, while, do/while Continue to execute until the condition is Fall 2016 CISC 1600 Yanjun Li 2 1
for Statement (1) Format: for (Initialization; Test; Update) //the block of code which is executed multiple times Initialization : Initial value of the control variable. Test : A boolean expression. Loop-continuation condition that tests for the final value of the control variable Update: Update the Variable. : Increment/decrement of control variable at each iteration Fall 2016 CISC 1600 Yanjun Li 3 for Statement (2) Assign 1 to counter counter <=10 cout Increment counter Fall 2016 CISC 1600 Yanjun Li 4 2
Question How many times your code is executed? for (int i = 0; i < 4; i++) cout<< loop A! <<endl; Assign 0 to counter counter < 4 cout Increment counter Fall 2016 CISC 1600 Yanjun Li 5 Question How many times your code is executed? for (int i = 1; i < 4; i++) cout<< loop B! <<endl; Assign 1 to counter counter < 4 cout Increment counter Fall 2016 CISC 1600 Yanjun Li 6 3
Question How many times your code is executed? for (int i = 1; i <= 4; i++) cout<< loop C! <<endl; Assign 1 to counter counter <= 4 cout Increment counter Fall 2016 CISC 1600 Yanjun Li 7 Examples Using the for Statement Vary control variable from 1 to 5 in increments of 1 for ( int i = 1; i <= 5; i++ ) Vary control variable from 5 to 1 in increments of -1 for ( int i = 5; i >= 1; i-- ) Vary control variable from 7 to 77 in steps of 7 for ( int i = 7; i <= 77; i += 7 ) Vary control variable from 20 to 2 in steps of -2 for ( int i = 20; i >= 2; i -= 2 ) Vary control variable over the sequence: 2, 5, 8, 11, 14, 17, 20 for ( int i = 2; i <= 20; i += 3 ) Vary control variable over the sequence: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0 for ( int i = 99; i >= 0; i -= 11 ) examplethree.cpp Fall 2016 CISC 1600 Yanjun Li 8 4
while Statement Action repeated while some condition remains. Format //condition variable is initiated while ( Condition ) // the block of code which is executed many times //condition variable is updated while loop repeats until condition becomes Fall 2016 CISC 1600 Yanjun Li 9 while Statement Pseudocode While there are more items on my shopping list Purchase next item and cross it off my list Example int listitem = 3; int shoppingcart = 0; while ( listitem > 0 ) shoppingcart++; listitem --; The variable involved in the condition must be updated inside the loop. Fall 2016 CISC 1600 Yanjun Li 10 5
while Statement Example: int value = 2; while ( value < 100 ) cout<< value << endl; value = value*2; Decision Logic A pretest loop value < 100 update value cout Fall 2016 CISC 1600 Yanjun Li 11 Example of Using while Statement bool flag = ; while (flag == ) int numberone; cout<< Please give me an integer : <<endl; cin >> numberone; if (numberone % 2 == 0) flag = ; What does this program do? (examplefour.cpp) Fall 2016 CISC 1600 Yanjun Li 12 6
Question Which should I use, while or for? while initialization; while ( loopcontinuationcondition ) ; update; for for ( initialization; loopcontinuationcondition; udpate ) ; The number of loops is known in advance. Fall 2016 CISC 1600 Yanjun Li 13 do while Statement do while Similar to while Tests loop-continuation after performing body of loop Loop body always executes at least once Fall 2016 CISC 1600 Yanjun Li 14 7
do while Statement Format //condition variable is initiated do // the block of s which is executed many times //condition variable is updated while ( Condition ); The block of code is always executed at least once. The variable involved in Condition must be updated inside the loop. Fall 2016 CISC 1600 Yanjun Li 15 do while Statement Example int value = 2; do cout << value is << value << endl; value = value * 2; while ( value < 100 ); Decision Logic A posttest loop print update value value < 100 Fall 2016 CISC 1600 Yanjun Li 16 8
break Statement Alter flow of control Causes immediate exit from control structure Used in while, for, do while or switch s Fall 2016 CISC 1600 Yanjun Li 17 Local Variable Variables declared in the loop structure only exist within the block. If the control variable is declared in the initialization expression of for It will be unknown outside the for Example ( exampletwo.cpp ): for (int counter = 1; counter<=4; counter++) int numberone = 4; cout << numberone * << counter << = << numberone*counter << endl; Fall 2016 CISC 1600 Yanjun Li 18 9
Non-local Variables (1) Variables declared outside the loop structure are non-local variables. The values of these variables are kept after finishing each iteration of the loop structure. Example: int sum=0; for (int i=0; i<5; i++) sum +=i; cout << sum of i after 5 iterations is << sum << endl; Fall 2016 CISC 1600 Yanjun Li 19 Non-local Variables (2) Another example ( exampleeight.cpp ): int cost = 0; for (int i=0; i<5; i++) int x, y; //two choices cout << enter the first choice <<endl; cin >> x; if (x == 1) cost += 100; else cost += 200; cout<< enter the second choice <<endl; cin >> y; if (y == 1) cost += 100; cout << cost for car no. << (i+1) << is $ <<cost<<endl; * Is this program correct? Fall 2016 CISC 1600 Yanjun Li 20 10
Reference Reproduced from the Cyber Classroom for C++, How to Program, 5/e by Deitel & Deitel. Reproduced by permission of Pearson Education, Inc. Fall 2016 CISC 1600 Yanjun Li 21 11