Download while

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Law of large numbers wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
Control Structures
Repetition
or
Iteration
or
Looping
Part I
Sequence
Print out a sales report
Open the salesperson file
Print heading on form
Skip 3 lines
Read the first record
Print salesperson’s name
Also called “Straight-Line” Programs
Decision (selection, branching)
Sequential
prior to if
{
sequential
yes
sales > quota
true
no
false
bonus = 0
bonus=sales*.01
inside an if
}
Sequential
after an if
b
l
o
c
k
Switch (Multiple Decision)
Can always be written as nested if
Option=1
Option=2
Option=3
Option=4
Assumes no multiple cases, break for each, no default
Repetition (iteration, looping)
More?
Yes true
Sequential
inside of
Comm = Sales*.02
the loop
Calculate Pay
Print Check
No
false
After the
loop,
continue
sequentially
with next
statement
The while Statement
Syntax
while (expression)
statement
Example:
count = 1;
while (count <= 10)
{
cout << “Yankees are #1\n”;
count = count + 1;
}
next statement
*
The while Statement
Syntax
while (expression)
statement


a loop control variable is evaluated
in the expression
the loop statements contain the lines
executed each time the loop repeats
The while Statement
Test the
expression
loop
1 or
True
statements
to execute
0 or
False
Exit the
while
Something to Note Note Note...
count = 1;
while (count <= 10)
{
cout << “Yankees are #1\n”;
cout << “and will win the series\n”;
}
next statement
How do we get out?
The while Statement





loop control variable is initialized before
while statement
evaluation or test is performed within the
expression
the body may contain any number of
statements, including branches and other
loops
the control variable is changed during
loop execution in order to exit loop
the statement immediately after the while
is executed upon exiting
*****
A Simple Example
int num;
cout << "NUMBER SQUARE CUBE\n"
<< "------ ------ ----\n";
num = 1;
while (num < 11)
{
cout << setw(3) << num << "
"
<< setw(3) << num * num << " "
<< setw(4) << num * num * num <<‘\n’;
num++;
// increment num
}
***
Simple Example Output
NUMBER SQUARE CUBE
-----------------------------------------------------------
1
2
3
1
4
9
1
8
27
10
100
1000
…
Another Example
double celsius, fahren;
cout << "CELSIUS FAHRENHEIT\n"
<< "------- ----------\n";
celsius = 5;
// starting Celsius value
while (celsius <= 50)
{
fahren = (9.0/5.0) * celsius + 32.0;
cout << setw(4) << celsius
<< setiosflags(ios::showpoint) << setw(13)
<< setprecision(2) << fahren << '\n';
celsius = celsius + 5;
}
Note: The text shows CONSTANTS in this
program on page 179
***
Problem Solving:
Finding the Largest Value




The program asks for the number of
items in the list.
Checks to see if that number is positive.
Gets user input.
Assigns the largest to variable max.
****
int count = 0, n = 0;
double max = 0, x = 0;
cout << "The maximum value will be computed.\n";
cout << "How many numbers do you wish to enter? ";
cin >> n;
while (n <= 0)
{ cout << "\nERROR: Positive integer required.\n\n”;
cout << "How many numbers to enter? ";
cin >> n;
}
cout << “Enter a real number: “;
cin >> x;
max = x;
// first value to max
****
while (count++ < (n-1)) // first n accepted above
{
cout << “Enter a real number: “;
cin >> x;
if (max < x)
max = x;
}
cout << “Maximum value: “ << max << “\n”;
}
Output
The maximum value will be computed.
How many numbers do you wish to enter? 4
Enter a real number: 1.01
Enter a real number: -3
Enter a real number: 2.2
Enter a real number: 7.07000
Maximum value: 7.07
***
n = 5;
count = 0;
cout << “Enter “ << n << …
cin >> x;
max = x;
// count = 0
1 while (count++ < (n-1))
2 {
3
cout << “LOOP number: “
cin >> x;
4
if (max < x)
5
max = x;
6 }
cout <<“Max is “<<max;
Counter++
loop
count n executed
1 5 1
2
2
3
3
4
4
5
*
Common Use: Running Totals
(Accumulator)
count =1; // variables must be initialized
total = 0; // before loop
while (count <=4)
{
cout << “Enter a number: “;
cin >> num;
total = total + num;
cout << “The total is “ << total;
count++;
}
*
count =1;
Common Use: Average
total = 0;
float average;
While (count <=4)
{
cout <<“Enter a number ”;
cin >> number;
total += number;
count++;
}
Why count – 1?
average = total / (count-1);
cout << “The average is “ << average << endl;
Sentinel Loops – exit controlled by
user
Different from fixed-count loops
Actually “variable-condition” loops
User can be asked: “Do you want to continue?”
Or, entry of unusual data value will end loop
Book example: Program 5-8 on page 189
A grades program, where entry of a grade
higher than 100 will exit the loop
Sentinel example
grade = 0; total = 0;
cout << “To stop, type number higher than 100”;
while (grade < 100)
{ total = total + grade;
cout << “Enter a grade: “;
cin >> grade;
}
cout << “\nThe total of the grades is “
<< total << endl;
Common Errors
Improper braces in nested structures
Using = in place of ==
!= versus == This changes the logic, be
especially careful when used with
&& or ||
infinite loops: != versus && versus ||
*