Introduction to Problem
Solving
g and Algorithm
g
Design
g
Topics
Asking for Help
• Asking
g for Help
p
• RAPTOR
• Chapter 3: Selection Structures:
Making Decisions
• Bad
–The program is giving me an error
–Giving
Gi i me th
the wrong iinformation
f
ti
• Good
–The error is "2345: Not Available"
–Here is a screenshot of the error
1-1
1-2
1-1
Example
• It keep
p on saying
y g that not enough
g
input to save
– savell "tip
"ti "name.lg
"
l
• This is correct,, I could not find error
• When recreated in class, student
was typing
i
– savel "tip"
tip name.lg
name lg
3-3
RAPTOR
Getting Started
• RAPTOR is a visual programming
p g
g
environment based on flowcharts
• RAPTOR ttakes
k flowcharting
fl
h ti one
p further by
y allowing
gy
you to test
step
(run) your program design and
confirm that your logic works
correctly to solve your problem.
• RAPTOR is on the CD
• The latest version is on the
website
b it
1-4
–http://raptor.martincarlisle.com
http://raptor.martincarlisle.com
• We will use the latest version
1-5
3-4
Can't we do this in PowerPoint?
1-3
1-2
1-6
3-5
Running RAPTOR
3-6
Adding Symbols – Just drag
• Yes – but RAPTOP knows about
flowcharts and won't let you make
mistakes.
mistakes
j
• PowerPoint is just
graphics, RAPTOR
is flowcharting
1-7
3-7
1-8
3-8
1-9
3-9
Edit the symbol
Let's Code the Music Program
1.
2.
3
3.
4.
5
5.
The Music Program in RAPTOR
print "Enter the number of songs: "
input Songs
set DollarPrice = 0.99
0 99 * Songs
print "Total price of songs is "
print
i t DollarPrice
D ll P i
1-10
1-11
3-10
We can Run it…
• Controls at top
• Input will be in
pop-up
pop
up windows
• Output will be in
master console
• Pretty cool,
cool eh?
3-12
Now do the TIP program
RAPTOR
• First let's do it without the total bill
• For more information,, read
Appendix D in your textbook
–Do it in RAPTOR and show me
–Sections
S ti
D.1
D 1 tto D
D.8
8
• And
A d then
h llets add
dd the
h totall bill
–Do
Do it in RAPTOP and show me
1-13
1-14
3-13
Chapter 3:
S l ti Structures:
Selection
St
t
Making
M ki Decisions
D i i
1-12
3-11
1-15
3-14
3.1 Introduction to Selection
St
Structures
t
3-15
Single Alternative
• Single-alternative (If-Then)
– A single block of statements to be executed or skipped
If something is true Then
Do something (any number
of
f statements)
)
End If
• Dual-alternative (If-Then-Else)
– Two blocks of statements, one of which is to be
executed, while the other one is to be skipped
• Multiple-alternative (If-Then-Else-If or
Case/Switch)
Write “How old are you?”
I
Input
t Age
A
If Age >= 18
Set Eligibility = “Yes”
Write “You can vote.”
Do other things…
End If
– More than two blocks of statements, only one of which
is to be executed and the rest skipped
Stewart Venit ~ Elizabeth Drake
Addison Wesley
is an imprint of
1-17
© 2011 Pearson Addison-Wesley. All rights reserved.
1-17
1-18
1-18
Dual Alternative
If something is true Then
Do something
El
Else
Do something else
End If
Write “How old are you?”
Input Age
If Age >= 18
Set Eligibility = “Yes”
Write “You can vote.”
Else
Flowchart for Single and Dual Alternative
Selection Structures
Flowchart for a Multiple Alternative
Selection Structure
Figure 3.1 Flowcharts for single- and dual-alternative
selection structures
Figure 3.2 Flowchart for a multiple-alternative selection structure
Set Eligibility
g
y = “No”
Write “You’re too young.”
End If
1-19
1-20
1-19
Guidelines
Relational Operators
• An Else condition does not have to exist.
Sometimes we only want to do something if
something is true and do nothing if it is not true.
• Do not manufacture alternative conditions.
• In an If statement, the body of the If is
executed if, and only if, the test condition is true.
Otherwise, no action is taken.
• Be sure to indent for readability.
• Do
D nott use the
th word
d Then
h after
ft an Else.
l
• Do use the word Then after an Else If.
Relational operators are the symbols used in the
condition to be evaluated in If statements:
1-22
==
!=
<
>
<=
>=
Assume the variables A and B have the values:
A = 9,
9 B = 6
Then the If statement:
If A > B Then
Write A + ”is greater than” + B
End If
can be read:
“If it is true that the value of the variable A is greater
than the value of the variable B,, then write the value
of B to the screen.”
The display will be:
9 i
is greater
t
th
than 6
i th
is
the same as (th
(the comparison
i
operator)
t )
is not the same as (not equal to)
less than
greater than
less than or equal to
greater than or equal to
1-23
A
A
A
A
A
A
> B is true
< B is false
>= B is true
<= B is false
<
!= B is true
== B is
i ffalse
l
1-25
This is demonstrated in the examples that follow in the next slides.
1-24
The Assignment Operator
Given: A = 14, B = 27
I programming
In
i code,
d th
the assignment
i
t statement:
t t
t
A =B
sets the value of B to the variable A.
A
In other words, after this statement is executed,
both A = 27 and B = 27
27.
In this case, the equals sign is used as an
assignment
g
operator.
p
There
e e is
s a ssignificant
g ca t d
difference
e e ce bet
between
ee tthe
e use o
of a
an
equals sign (=) as the assignment operator and a double
equals sign (==) as the comparison operator.
As an assignment
g
operator,
p
, the equals
q
sign
g sets the value
of an expression on the right side to the variable on the
left side.
As a comparison
p
operator,
p
the double equals
q
sign
g asks
the question, “Is the value of the variable on the left side
the same as the value of the expression, number, or
variable on the right side?”
• a single
i l equals
l sign
i ((=)) signifies
i ifi th
the assignment
i
t operator
t
• a double equals sign (==) signifies the comparison
operator
1-25
1-24
1-23
Comparison vs. Assignment
O
Operators
t
Given: A = 23, B = 16
Then:
1-21
Example
p
1-22
More Examples
1-21
1-20
1-26
1-26
1-27
1-27
The Comparison Operator
Two Ways to Obtain a Positive Result
Given: A = 14, B = 27
U i relational
Using
l ti
l operators,
t
th statement:
the
t t
t
A == B
i a comparison.
is
i
This statement asks the question, “Is the value of A
the same as the value of B?
B?” In this case,
case since A
and B have different values, the answer is “no”
and the statement would result in a value of
False.
l
Both of the following examples will give the
same result:
lt
But be careful…
Do these give the same result?
If Age > 16 Then
Write “You can drive!”
Else
Write “Sorry, you’re too young.”
End If
If Number < 0 Then
Set PosNum = -Number
Write PosNum
Else
Write Number
End If
If N
Number
b
> 0 Th
>=
Then
Write Number
Else
Set PosNum = -Number
Write PosNum
End If
1-28
Why
y are these
not equivalent?
If Age < 16 Then
Write “Sorry, you’re too young.”
Else
Write “You can drive!”
End If
1-29
1-28
Combining Logical and Relational Operators to
Create Compound Conditions
Logical Operators
• Logical operators are used to connect
simple conditions into a more complex
condition called a compound
condition.
• The
Th simple
i l conditions
diti
each
h contain
t i
one relational operator.
• Using compound conditions reduces
the amount of code that must be
written.
1-30
1-29
Hints
• In a compound
p
condition,, it is
necessary to use complete simple
conditions.
conditions
• This is correct:
This code is
equivalent to   
Input X
If X < 5 Then
Write “OK”
End If
If X > 10 Then
W it “OK”
Write
End If
this code. But this code is
shorter!
Input X
If (X < 5) OR (X > 10) Then
Write
te “OK”
O
End If
1-31
1-30
If (X < 5) OR (X > 10) Then
…
1-32
1-31
1-32
• This is not correct:
If (X < 5 OR > 10) Then …
1-33
1-33
The AND Operator
The OR Operator
The NOT Operator
• A compound condition consisting of two
simple conditions joined by an AND is true
only if both simple conditions are true. It is
false if even one of the conditions is false.
The statement:
If (X > 5) AND (X < 10) Then …
is true only if X is 6, 7, 8, or 9. It has to be
both greater than 5 and less than 10 at the
same time.
• A compound condition consisting of two
simple conditions joined by an OR is true if
even one of the simple conditions is true. It
is false only if both are false. For example:
• AND and OR affect 2 simple conditions.
• NOT affects only one condition. If you need to
negate more than one simple condition, you will
need more than one NOT
• A condition with the NOT operator is true only if
the condition is false.
NOT(A < B)
is true only if B is greater than or equal to A.
If (X > 100) AND NOT(X == Y) Then…
is true only if X is greater than 100 but not equal
t the
to
th value
l off Y.
1-36
If (Response ==“Y”) OR (Response ==“y”) Then
…
1-34
1-34
• This is true if Response is uppercase (‘Y’)
or lowercase
l
(‘
(‘y’).
’) F
For the
th above
b
condition
diti
to be false, Response would have to be
something
thi other
th th
than either
ith ‘Y’ or ‘‘y’.’
1-35
1-35
1-36
Truth Tables for OR, AND, and NOT
O
Operators
t
Type
X
Y
X OR Y
X AND Y
NOT X
true
true
true
true
false
true
false
true
false
false
false
true
true
false
true
false
false
false
false
true
Arithmetic
operations
are performed first, in
order
d shown
h
Relational
operations
are performed second
Logical operations
are performed last, in
the order shown
Operator
Order Performed
( )
^
* / %
+ -
1st parentheses
2nd exponentiation
3rd: multiplication,
division modulus
division,
4th: addition,
subtraction
== !=
!
> >=
NOT
AND
OR
< <
<=
An employee is not eligible for overtime
pay if he has worked less than 40
hours in a week or if his pay rate is
greater than $10.00/hour.
$
This
problem can be worked two ways,
using either a compound condition
with the AND operator or a compound
condition with an OR operator, as
shown by the two flowcharts on the
next slides.
Let Q = 3 and let R = 5
Is the following expression true or false?
NOT Q > 3 OR R < 3 AND Q – R <= 0
Step 1
S
1: (NOT(false)) OR (false AND true)
Step 2: true OR (false AND true)
Step 3: true OR false
Step 4: true
1st: NOT
(!)
(&&)
3rd: OR (||)
2nd: AND
1-38
1-37
Example: Pay with Overtime
Example:
p
All relational operators
have equal
precedence
1-37
Using
g the OR operator to solve
the Pay with Overtime Problem
1-40
Using
g the AND operator
p
to solve
the Pay with Overtime Problem
1-42
1-41
Ordering Arbitrary Strings
• All data, including characters, are stored in the
p
memory
y in binary
y form ((zeros and ones).
)
computer’s
• A programming language uses a scheme to associate
each character with a number.
• Standard is the American Standard Code for
Information
Interchange (ASCII code) -- pronounced “askey.”
• Each character is associated with a number from 0 to
127.
• Letters are in alphabetical order
• All uppercase letters
l tt
precede
d allll llowercase
letters.
• Digits (viewed as characters) retain their natural
order. For example, “1” < “2”, “2” < “3”
precedes all digits
g and letters.
• The blank p
Examples:
g conditions are true:
All of the following
–“a” > “B”
–“1” <= “5”
–“2” >= “2”
1-43
1-39
1-41
3.3 ASCII Code
1-43
1-39
1-38
1-40
Examples:
• Uppercase A is 65
• Uppercase Z is 90
• digits have codes from 48 (“0”) to 57 (“9”)
Combining Logical and Relational
Operators
Hierarchy of Operations
1-42
3.4 Selecting from Several
Alt
Alternatives
ti
• Sometimes, we must handle more than two options in a
program We can use multiple If
program.
If-Then
Then statements or
multiple If-Then-Else statements.
If something is true Then
Do something
Else
If something else is true Then
Do something else
Else
Do a different something else
End If
End If
1-44
1-44
1-45
1-45
Example
If Age >= 18 Then
Set Eligibility = “Yes”
Yes
Else
If Age > 15 Then
Set Eligibility = “Maybe”
Else
S t Eligibility
Set
Eli ibilit = “No”
“N ”
End If
End If
Hints
Case-type Statements
• The number of End If’s must equal the
number
b off If’s
’ .
• You can draw a line to connect them to
check.
h k
• In the previous example, the check for Age
> 15 will
ill never b
be d
done if th
the Age
A is
i >
18.
• Regardless
R
dl
off h
how many possible
ibl
conditions are included, only one will ever
be executed.
executed
• Case or Switch statements can be used to more easily
code multiple alternative If
If’s
s
• Use the special or keyword Case
• Use Select to start the Case statement
• Specify the expression to be evaluated
• List each possible value of the expression and what to do
if that is that value is the true one
• A Break at the end of each Case tells the computer to
skip the rest of the conditions
• Use
U End
E d Case
C
t end
to
d the
th statement
t t
t
• The results will be the same as a correctly coded
multiple-alternative If-Then-Else structure
1-46
1-47
1-46
Example
Select Case of Choice
Case 1:
Set Operation
Break
Case 2:
Set Operation
Break
Case 3:
Set Operation
Break
Case 4:
Set Operation
Break
End Case
3.5 Applications of Selection
Structures
= “Subtract”
= “Multiply”
= “Divide”
1-49
Input Cost
If Cost <= 0 Then
Write “Invalid
Invalid cost”
cost
Else
Write “The
The cost is ” + Cost
End If
1-49
Avoid Illegal Operations with Sqrt()
Example: The reciprocal of a number is 1 divided by that
number.
b H
However, since
i
di
division
i i b
by zero iis nott allowed,
ll
d
the reciprocal of 0 is undefined. The following example
will display the reciprocal of any number:
Write “Enter a number.”
Write “This program will display its reciprocal.”
Input Number
If Number != 0 Then
Set Reciprocal = 1/Number
Write “The reciprocal of “+Number+“ is “+Reciprocal
Else
Write “The reciprocal of 0 is not defined.”
End If
1-50
1-52
1-52
1-51
Menu-Driven
Menu
Driven Programs
• Be sure to test your program by ‘playing
computer ’
computer.
• This is also called ‘desk checking.’
• Perform all calculations multiple times manually
or with a calculator.
• Use data that will show the results when each
branch of each selection structure is executed at
least once.
• Check
Ch k ffor di
division
i i b
by zero, negative
ti values
l
ffor a
square root function, and any other special
conditions.
Write “Enter
“
a number.”
”
Write “This program will display its square root.”
Input Number
If Number >= 0 Then
Write “The square root of “ + Number + “ is “+ Sqrt(Number)
Else
Write “The square root of “ + Number + ” is not defined.”
End If
1-51
1-50
Defensive Programming
The following program segment displays the square
root of a number unless the number is negative:
1-48
An Error Trap for a Division by Zero Error
• Program defensively in order to prevent bad data from
entering our program. To do this, set error traps.
• If our program should accept only a Cost greater than
0, we can stop any other value from entering with the
following trap:
= “Add”
1-48
1-47
• A major goal of a programmer is to create user friendly
programs.
• When the user has many options, menus are often
used.
• Such programs are menu-driven.
• Menus are usually arranged in a row near the top of the
screen.
• The user clicks the mouse on the desired choice.
• The main menu (a list of the program’s major functions)
is usually the first thing the user sees.
1-53
1-53
1-54
1-54
Details on the Exam
Closing Thought
•
•
•
•
• "Any
Any fool can write code that
a computer can understand.
Good programmers write
code that humans can
understand.
understand."
Some Multiple
p Choice q
questions
Some code analysis
Some flowcharting
Chapters 0
0-3,
3 D.1
D 1 to D
D.8
8
End of this Lecture
- Martin Fowler
1-55
3-55
1-56
1-56
1-57
1-57