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
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 DocumentRight Arrow Icon
Reminder: Control Structures lmost all of our demo programs so far have taken this 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 teration until a particular condition is satisfied. Iteration 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; ut what if we want to use the program for courses with a But what if we want to use the program for courses with a different number of assignments? ow can e modify this program to handle e requirement 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 DocumentRight 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. nt core; int score; int total = 0; cout << "Enter cores. T rminate ith a negative integer: "; gg : ; cin >> score; if( score >= 0){ otal total + core; in > core; total = total + score; cin >> score; } if( score >= 0){ otal total + score; in > score; But how many times should we total = total + score; cin >> score; } if( score >= 0){ otal total + score; in > 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. e can write our rogram s: We can write our program as: nt core; int score; int total = 0; cout << "Enter scores. Terminate with a negative integer: "; 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 DocumentRight Arrow Icon
While loop the body will not execute t all the condition When executing a while loop at all if the condition evaluates false on the first attempt the condition is evaluated first . if the condition is false , the body of the loop is not xecuted It is skipped and the hile loop rminates executed. It is skipped and the while loop terminates. if the condition is true , the body is executed once, then the whole while loop is executed again. That is, the condition is evaluated again, etc.
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 36

CP1_5_Iteration_1112 - Fl Flow of Control 2 Iteration...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online