Chapter 2 - Algorithms and Design: Counter Loop User Query Loop Sentinel Value Loop
Chapter 2 - Algorithms and Design: Counter Loop User Query Loop Sentinel Value Loop
print Statement
input Statement and Variables
Assignment Statement
if Statement
Flowcharts
Flow of Control
Looping with Flowcharts
Looping with Pseudocode
Tracing
Pseudocode Syntax Summary
Loop Terminatation
Counter loop
User query loop
Sentinel value loop
Nested Loops
1
print Statement
Here's a pseudocode algorithm that calculates the area of a
rectangle: print statement
print "Enter a length: "
input length
print "Enter a width: "
input width
set area to length * width
print "The area is " area
• The first two print statements are called prompts because they
prompt the
3 user to enter a value.
Assignment Statement
8 2 * 4
4
if Statement
Use an if statement if you need to ask a question in order to determine
what to do next.
There are three forms for an if statement:
“if”
“if, else”
“if, else if”
6
if Statement
Practice problems:
Write an algorithm that prints "warm" if the temperature (a variable) is
above 50 degrees and prints "cold" otherwise.
Write an algorithm that prints "No school!" if the temperature is below 10
degrees.
Write an algorithm that prints "too cold" if the temperature is below 50
degrees, "OK" if the temperature is between 50 and 90 degrees, and "too
hot" if11the temperature is above 90 degrees.
if Statement
• Write an algorithm that prints "too cold" if the temperature
is below 50 degrees, "OK" if the temperature is between 50
and 90 degrees, and "too hot" if the temperature is above 90
degrees.
14
Flowcharts
15
Flowcharts
Equivalent flowchart:
• Example algorithm that cuts a CEO’s print "Enter CEO
Salary:"
500000 yes
set ceoSalary to
set ceoSalary to ceoSalary ceoSalary * .5
* .5
print "Reduced CEO
ceoSalary
16
Flowcharts
Flowchart:
17
Flow of Control
19
Looping with Flowcharts
Flowchart:
• Let's first implement a loop
using a flowchart (later
we'll implement loops
using pseudocode):
• Draw a flowchart that
prints "Happy birthday!"
100 times.
• Note: We don't need any
new flowchart symbols for
loops. Flowchart looping is
implemented by an arrow
going to a previously
executed statement.
20
Looping with Pseudocode
• Use a loop statement if you need to do the same thing
repeatedly.
• loop format:
the loop's heading
while <condition>
the loop's body
<statement(s)>
• Loop terminology:
• The number of times that the loop repeats is called the number of
iterations.
• It's possible for a loop to repeat forever. That's called an infinite loop.
(Note: It's also possible for a loop to repeat zero times.)
• Example:
• Write an algorithm that prints "Happy Birthday!" five times.
set count to 1
while count is less than or equal to 5
print "Happy birthday!"
set
23
count to count + 1
What is tracing? Tracing
It's when a human executes an algorithm (or a program) line by line and
carefully records everything that happens.
It's used to 1) verify that an algorithm is correct or 2) find errors/bugs in an
algorithm.
How to trace:
Setup:
If there is input, provide a column heading labeled "input."
Provide a column heading for each variable.
Provide a column labeled "output."
Whenever there's an input statement, cross off the next input value under
the input column heading.
Update a variable's value by writing the new value underneath the variable's
column heading and crossing off the old value.
Whenever there's a print statement, write the printed value under the
output column heading.
For full
24 credit on the homework, your variable and output values must be
100% accurate.
Pseudocode Summary
• print <variables, strings, math expressions>
• Use quotes to surround strings.
• input <variable>
• Variables (no spaces, all lowercase except for first letter of 2nd,
3rd, etc. words)
• Assignment statement
• set <variable> to <value>
• math operators: +, -, /, *
• if <condition>
• else if <condition>
• else
• while <condition>
25
Loop Termination
• There are three basic ways to terminate/exit loops:
• Counter loop
• Use a counter variable to keep track of the number of iterations.
• Example - "Happy birthday" algorithm.
• User query
• Ask the user if he/she wants to continue.
• Example – miles-to-kilometers conversion algorithm (coming up).
• Sentinel value
• Use a special value to indicate that there's no more input.
• Example - bowling scores algorithm (coming up).
26
User-Query Example
• Write an algorithm that converts a series of user-entered
miles values to kilometer values. The program should
continue as long as the user answers "y" to a "Continue?"
prompt.
set continue to "y"
while continue equals "y"
print "Enter a distance in miles: "
input miles
set km to miles * 1.609
print "The equivalent distance in kilometers is " km
print "Continue? (y/n): "
input27continue
Sentinel Value Example
28
Sentinel Value Example
31
Nested Loops
• A nested loop is a loop that's inside another loop.
• Example:
• Write an algorithm that finds the largest prime number from a list
of user-entered numbers. The user indicates he/she is done by
entering a negative number.
• For each entered number, how can you determine whether the
number is prime?
• When the user finally enters a negative number, the algorithm
prints the largest prime number entered or “No prime numbers
were entered.”
32
set largestPrime to 1 Nested Loops
print “Enter a number (negative to quit): ”
input num
while num ≥ 0
set count to 2
set prime to “yes”
while count < num and prime equals “yes”
if num / count has no remainder
set prime to “no”
else
set count to count + 1
if prime equals “yes” and num > largestPrime
set largestPrime to num
print “Enter a number (negative to quit): ”
input num
if largestPrime equals 1
print “No prime numbers were entered.”
else
33
print “The largest prime number entered was ” largestPrime “.”