Professional Documents
Culture Documents
ENGR 1200U Introduction To Programming: Consider The Following Example
ENGR 1200U Introduction To Programming: Consider The Following Example
1992-2012 by Pearson Education, Inc. & John Wiley & Sons Some portions are adopted from C++ for Everyone by Horstmann
int number(159); int sum=0; while (number>0) { int rem=number%10; sum=sum+rem; number=number/10; } cout<<"Thesumis:" <<sum<<endl;
sum
number
N/A 9 5 1
0 9 14 15
159 15 1 0
It often happens that you want to execute a sequence of statements a given number of times. You can use a while loop, controlled by a counter
int counter=1; while (counter<=10) { cout<<counter<<endl; counter++; }
//initializecounter //checkcounter //printcountervalue //updatecountervalue
initialization expression
condition expression
Update expression
update
initialization expression
condition expression
Update expression
initialization expression
condition expression
Update expression
initialization expression
condition expression
Update expression
int main() { const double RATE=5; const double INITIAL_BALANCE=10000; double balance=INITIAL_BALANCE; int number_of_years; cout <<"Enterthenumberofyears:"; cin >>number_of_years;
Problem: Print the balance of our savings account over a period of years Solution: Use for loop
cout <<fixed<<setprecision(2); cout <<setw(4)<<"Year" <<setw(10)<<"Balance" <<endl; for (int year=1;year<=number_of_years;year++) { double interest=balance*(RATE/100); balance=balance+interest; cout <<setw(4)<<year<<setw(10)<<balance<<endl; } Algorithm: return 0; }
for (int year=1;year<=number_of_years;year++) { //updatebalance //printyearandbalance }
C++ for Everyone by Cay Horstman
output
012345
output
output
02468
There are 5 iterations
output
024681012
infinite loop; never ends
Sometimes you want to execute the body of a loop at least once and perform the loop test after the body is executed. The do/while loop serves this purpose.
do {
//statements Use the do/while loop when statements must be executed before any knowledge of the condition
}while(condition);
At what instances would you require something to take place before executing a loop?
Prompttheuserforinput
Example 1
int value; do { cout<<"Enteravalue<100:"; cin>>value; } while (value>=100);
Loops are often required for reading data from the keyboard or from a data file There are three common loop forms
1. CounterControlled Loop 2. SentinelControlled Loop 3. EndOfData Loop
10
Used for reading input data if the number of data values is known before the data are entered
Number of data values is stored in a counter
counter is often used to control the number of iterations can be easily implemented using the while or for loops
Used for reading input data if a special data value exists that can be used to indicate the end of the data
When reading a sequence of inputs, you often need a method to indicate end of the sequence A value that serves as a signal for termination is called a sentinel
11
Assume we would like compute the average of a set of salary values. We will use -1 as a sentinel.
Inside the loop, we read an input. If the input is not -1, we process it. To compute the average, we need the total sum of all salaries, and the number of inputs.
while (...) { cin>>salary; if (salary!=1) { sum=sum+salary; count++; } } while (salary!=1) { cin >>salary; if (salary!=1) { sum=sum+salary; count++; } }
There is one problem: when the loop is entered the first time, no data value have been read.
Hence, no initial value for salary
doublesalary=0; while (salary!=1) { cin>>salary; if (salary!=1) { sum=sum+salary; count++; } }
12
Is the most flexible loop for reading input data It is structured to continue executing the statements inside the loop while new data are available
No prior knowledge values is required of data
13