Presentation is loading. Please wait.

Presentation is loading. Please wait.

while Repetition Structure

Similar presentations


Presentation on theme: "while Repetition Structure"— Presentation transcript:

1 while Repetition Structure
Action repeated while some condition remains true Pseudocode while there are more items on my shopping list Purchase next item and cross it off my list while loop repeated until condition becomes false Example int product = 2; while ( product <= 1000 ) product = 2 * product;

2 The while Repetition Structure
Flowchart of while loop product <= 1000 product = 2 * product true false

3 Formulating Algorithms
Example A class of ten students took a quiz. The grades (integers in the range 0 to 100) for this quiz are available to you. Determine the class average on the quiz.

4 Formulating Algorithms
Pseudocode for example: Set total to zero Set grade counter to one While grade counter is less than or equal to ten Input the next grade Add the grade into the total Add one to the grade counter Set the class average to the total divided by ten Print the class average Next: C++ code for this example

5 // Program to calculate a class average
#include <iostream> // function main begins program execution int main() { double total; // sum of grades input by user int gradeCounter; // number of grade to be entered next double grade; // grade value double average; // average of grades // initialization phase total = 0; // initialize total gradeCounter = 1; // initialize loop counter // processing phase while ( gradeCounter <= 10 ) { // loop 10 times cout << "Enter grade: "; // prompt for input cin >> grade; // read grade from user total = total + grade; // add grade to total gradeCounter = gradeCounter + 1; // increment counter } // termination phase average = total / 10; // calculate average // display result cout << "Class average is " << average << "\n"; return 0; // indicate program ended successfully } // end function main


Download ppt "while Repetition Structure"

Similar presentations


Ads by Google