5-Short_Iteration_2

5-Short_Iteration_2 - C+ Programming Control Structures,...

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

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

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

View Full DocumentRight 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
Background 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
Background image of page 3

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

View Full DocumentRight Arrow Icon
Background 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
Background image of page 5

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

View Full DocumentRight 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) ;
Background image of page 6
Background image of page 7

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

View Full DocumentRight Arrow Icon
Background 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 ; }
Background image of page 9

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

View Full DocumentRight 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 ; }
Background 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.
Background image of page 11

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

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

Page1 / 52

5-Short_Iteration_2 - C+ Programming Control Structures,...

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

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