{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CP1_5_Iteration_1112

CP1_5_Iteration_1112 - Fl Flow of Control 2 Iteration...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Fl f C t l 2 Flow of Control 2 Iteration
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Reminder: Control Structures Almost all of our demo programs so far have taken this form – sequences of executable statements. Sequence Where certain statements or sequences of statements are selected for execution only if particular conditions are satisfied. Decision Where sequences of statements are executed repeatedly Iteration until a particular condition is satisfied. In this class we cover the third of these: Iteration (also known as Looping ) 2 CSIS1117B Computer Programming 1 2011-2012
Background image of page 2
Looping constructs As a simple introduction, let’s say we want to find the sum of 3 integer assignment scores input by the user. We could write the program as: int score; int total = 0; cout << "Enter 3 scores as integers: "; cin >> score; total = total + score; cin >> score; total = total + score; cin>> score; total = total + score; cout << "The sum of scores is " << total << endl; But what if we want to use the program for courses with a different number of assignments? How can we modify this program to handle the requirement to find the sum of an arbitrary number of assignment scores? 3 CSIS1117B Computer Programming 1 2011-2012
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Looping constructs Users can enter as many scores as they want. What is important for us is to know when they are finished. Specify that they will end by entering a value of less than zero. int score; int total = 0; cout << "Enter scores. Terminate with a negative integer: "; cin >> score; if( score >= 0){ total = total + score; cin >> score; total = total + score; cin >> score; } if( score >= 0){ total = total + score; cin >> score; But how many times should we total = total + score; cin >> score; } if( score >= 0){ total = total + score; cin >> score; do this? We don’t know. total = total + score; cin >> score; } 4 CSIS1117B Computer Programming 1 2011-2012
Background image of page 4
“While” loop A while loop – provides a way to execute one or a block of statements (the body of the loop) repeatedly while a condition is satisfied. We can write our program as: int score; int total = 0; cout << "Enter scores. Terminate with a negative integer: "; i i i li ' f cin >> score; while ( score >= 0 ) { Condition Must initialize. Don't forget! total = total + score; cin >> score; } Body cout << "The sum of scores is: " << total << endl; 5 CSIS1117B Computer Programming 1 2011-2012
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon