Title: General
1 On the menu today I- Quick review II- Control
Structure continue III- Assignment
continue IV- Math library
2I- Review
3- Relational logical operators
4 Goal Compare or Write conditions Relational
expression operand operator operand
Goal Create more complex conditions
5- C- Expression value and interpretation
relational and logical expression
take value 1 0 interpreted in true false
6- Program Control Structure
7- Sequence
- Selection
- Iteration
8// Is this partial code correct ? x 0 if (x
0) cout // Correction x 0 if (x 0) cout Happy Birthday"
9- II- Program Control Structure
- Iteration continue
10 Let's write a program to print out a chart of
the first 10 positive integers and their
squares.
11- A- Iteration why we need loops
include int main() cout
ut cout out cout
12 Alternative? x ? 0 loop while x 1 to x report x and x2 end loop
13- A- Iteration why we need loops
Observations? a) we have found the pattern
and only written that b) we have let the
computer do the work
14- B- Iteration Type of loops
Try to write the problem solution statement
for averaging the ages of the students in the
class, knowing the number of the students
15- B- Iteration Type of loops
Without a loop ageTotal ? 0 studentCount ?
0 input age add age to ageTotal add 1 to
studentCount input age add age to
ageTotal add 1 to studentCount ... averageAge
? ageTotal / studentCount report averageAge
16- B- Iteration Type of loops
How about when the students are coming through
the door one at a time so you don't know how
many there are going to be?
17- B- Iteration Type of loops
Without a loop ageTotal ? 0 studentCount ?
0 input age add age to ageTotal add 1 to
studentCount input age add age to
ageTotal add 1 to studentCount ... averageAge
? ageTotal / studentCount report averageAge
NOT POSSIBLE WITHOUT A LOOP
18- C- Iteration Counted loops
Used when we know ahead of the loop how many
times it will execute. Case1 Suppose we know
that there are 43 students in the class
19- C- Iteration Counted loops
loopCounter ? 0 studentCount ? 0 ageTotal ?
0 loop while loopCounter studentAge add studentAge to ageTotal add 1 to
studentCount add 1 to loopCounter end
loop averageAge ? ageTotal / studentCount report
averageAge
How many times?
How many times?
How many times?
20- C- Iteration Counted loops
Case 2 Suppose we don't know how many
students are in the class but we can ask
So if we ask we will know how many times the
loop will be computed!
21loopCounter ? 0 studentCount ? 0 ageTotal ?
0 display "How many students?" input x loop
while loopCounter studentAge to ageTotal add 1 to
studentCount add 1 to loopCounter end
loop averageAge ? ageTotal / studentCount report
averageAge
How many times?
How many times?
How many times?
22- C- Iteration Counted loops
USE COUNTED LOOPS WHEN THE NUMBER OF
ITERATIONS IS KNOWN OR CAN BE COMPUTED OR INPUT
23- D- Iteration Indefinite loops
Suppose we want to print out our table of
integers and their squares until the square gets
larger than 500 (as long as the square is less
than or equal to 500).
24- D- Iteration Indefinite loops
x ? 0 loop while x2 x report x and x2 end loop
25- E- Iteration Sentinel loops
Let's count the number of students in a class
without knowing in advance how many there are.
Imagine they pop through the door one at a
time, state their age, and go sit down. Barney
miraculously comes through after the very last
student. Barney doesn't count as a student
Barney is only there to let you know that there
aren't any more students. Note the fine
distinction between a student and Barney -)
26- E- Iteration Sentinel loops
studentCount ? 0 input student loop while
student not Barney add 1 to studentCount input
student end loop report studentCount
How many times?
How many times?
How many times?
27- E- Iteration Sentinel loops
How many times do we input student? How many
of those are really students? How many of those
are Barney?
28- E- Iteration Sentinel loops
Instead of Barney, we'll agree on a particular
value for some variable that allows us to
recognize Barney's presence a sentinel
value. For instance, we'll agree that a
negative value for age is the sentinel.
29- E- Iteration Sentinel loops
ageTotal ? 0 studentCount ? 0 input
studentAge loop while studentAge 0 add
studentAge to ageTotal add 1 to
studentCount input studentAge end
loop averageAge ? ageTotal / studentCount repor
t averageAge
How many times?
How many times?
How many times?
30- E- Iteration Sentinel loops
How many times do we input studentAge? How many
of those ages are for real students? How many
ages are Barney? (the sentinel value?)
31 x ? 0 loop while x2 loop
What is wrong?
32 ageTotal ? 0 studentCount ? 0 input
studentAge loop while studentAge 0 add
studentAge to ageTotal add 1 to studentCount end
loop averageAge ? ageTotal / studentCount report
averageAge
What is wrong?
33This is the correct version ageTotal ?
0 studentCount ? 0 input studentAge loop while
studentAge 0 add studentAge to ageTotal add 1
to studentCount input studentAge end
loop averageAge ? ageTotal / studentCount report
averageAge
Is this Correct?
34 Notice that even when we find Barney, the rest
of the loop will execute!!!
ATTENTION this is incorrect ageTotal ?
0 studentCount ? 0 loop while studentAge
0 input studentAge add studentAge to
ageTotal add 1 to studentCount end
loop averageAge ? ageTotal / studentCount report
averageAge
35 ageTotal is behaving as an "accumulator" studen
tCount is behaving as a "counter"
36- H- Iteration Syntax Example
C syntax for loops the while
statement while (condition) statement
37- H- Iteration Syntax Example
- Pseudocode
- x ? 0
- loop while x
- x ? x 1
- report x and x2
- end loop
- C
- x 0
- while (x
-
- x x 1
- cout
-
38- H- Iteration Syntax Example
float n n 0.0 while (n n
39- H- Iteration Syntax Example
float n, k n 0.0 k 0.0 while (n 4.0) while ( k
40include void main() int
loopCounter, studentCount, studentAge float
ageTotal, averageAge loopCounter 0
studentCount 0 ageTotal 0 while
(loopCounter " cin studentAge ageTotal ageTotal
studentAge studentCount studentCount
1 loopCounter loopCounter 1
averageAge ageTotal / studentCount cout "The average age is "
41include void main() int
loopCounter, studentCount, studentAge float
ageTotal, averageAge int x
loopCounter 0 studentCount 0
ageTotal 0 cout " cinx while (loopCounter cout
studentAge ageTotal ageTotal
studentAge studentCount studentCount
1 loopCounter loopCounter 1
averageAge ageTotal / studentCount cout
42include void main() int
studentCount, studentAge, studentCount float
ageTotal, averageAge studentCount 0
ageTotal 0 cout " cin studentAge while (studentAge
0) ageTotal ageTotal
studentAge studentCount studentCount
1 loopCounter loopCounter 1 cout "Enter an age " cin studentAge
averageAge ageTotal / studentCount cout "The average age is "
43(No Transcript)