{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

5-Short_Iteration_2

5-Short_Iteration_2 - C Programming Control Structures...

Info icon This preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
C++ Programming Control Structures, Repetition (Loop)
Image of page 1

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

View Full Document Right Arrow Icon
Why Is Repetition Needed? Repetition allows you to efficiently use variables Can input, add, and average multiple numbers using a limited number of variables For example, to add five numbers: Declare a variable for each number, input the numbers and add the variables together Create a loop that reads a number into a variable and adds it to a variable that contains the sum of the numbers
Image of page 2
The while Loop The general form of the while statement is: while (bool expression) Task ; while is a reserved word The parentheses are part of the syntax Expression acts as a decision maker and is usually a logical expression Statement can be simple or compound Statement is called the body of the loop
Image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
The while Loop (continued) Initialize condition ; while ( check Condition ) { Task ; Reset condition ; } Continue ; Expression provides an entry condition Statement executes if the expression initially evaluates to true Loop condition is then reevaluated Statement continues to execute until the expression is no longer true
Image of page 5

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

View Full Document Right Arrow Icon
The while Loop (continued) Infinite loop : continues to execute endlessly Condition is not reset in the loop Can be avoided by including statements in the loop body that resets the condition to assure exit condition will eventually be false Proper resetting of the condition Semicolon after the parenthesis while( condition) ;
Image of page 6
Image of page 7

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

View Full Document Right Arrow Icon
Image of page 8
// Write a program to read 5 test scores and print the // sum and average of the Scores. #include <iostream> #include <iomanip> using namespace std ; int main() { float Score1, Score2, Score3, Score4, Score5, Average,Total; Total = 0 ; cout << fixed << left << setprecision(3) << showpoint ; cout << “Enter 5 test score separated by a space: “; cin >> Score1 >> Score2 >> Score3 >> Score4>>Score5 ; Total = Score1 + Score2 + Score3 + Score4 + Score5 ; Average = Total / 5 ; cout <<endl << setw(30)<< “The sum of scores is: “<< Total ; cout << setw(30)<<“The average of scores is: “ << Average ; return 0 ; }
Image of page 9

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

View Full Document Right Arrow Icon
#include <iostream> #include <iomanip> using namespace std ; int main() { float Score, Average,Total; int I = 0 ; Total = 0 ; cout << fixed << left << setprecision(3) << showpoint ; cout << “Enter 5 test score separated by a space: “; while ( I < 5 ) { cin >> Score ; Total+= Score ; I ++ ; } Average = Total / 5 ; cout <<endl << setw(30)<< “The sum of scores is: “<< Total ; cout << setw(30)<<“The average of scores is: “ << Average ; return 0 ; }
Image of page 10
Using a loop to sum scores and finding average Using Conditional loop // Write a program to read a list of test scores and print the sum and average of the Scores. // if a negative test score is entered stop the process and print the sum and average. float Score, Total, Average = 0 ; int Counter; Total = 0 ; cout << fixed << left << setprecision(3) << showpoint ; Counter = 0 ; cout << “Please enter first score or a negative score to stop processing: ”; cin >> Score ; while (Score >= 0 ) { Total += Score; cout << “Please enter another score or a negative score to stop processing: ”; cin >> Score ; Counter ++ ; } if ( Counter > 0 ) { Average = Total / Counter ; cout <<endl << setw(30)<< “The sum of “<<Counter << “ scores is: “<< Total ; cout << setw(30)<<“The average of “ << Counter <<“ scores is: “ << Average ; } else cout << “\n************** No scores were entered.\n” ;
Image of page 11

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

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern