Programming Chap 4
Programming Chap 4
Cold True
Decision structure
outside
Wear a coat.
False
Sequence structure
Go outside
End
The flowchart in the figure starts with a sequence
structure. Assuming you have an outdoor thermometer in
your window, the first step is Go to the window, and the
next step is Read thermometer. A decision structure
appears next, testing the condition Cold outside. If this is
true, the action Wear a coat is performed. Another
sequence structure appears next. The step Open the door
is performed, followed by Go outside.
Cold True
outside
Decision structure Wear a coat
Wear gloves
Writing a Decision Structure in Pseudocode
If condition Then
statement
statement These statements are conditionally
executed.
etc.
End If
Boolean Expressions and Relational Operators
All programming languages allow you to create expressions
that can be evaluated as either true or false. These are called
Boolean expressions. Typically, the Boolean expression that is
tested by an If-Then statement is formed with a relational
operator.
A relational operator determines whether a specific
relationship exists between two values. For example, the
greater than operator (>) determines whether one value is
greater than another. The equal to operator (==) determines
whether values are equal.
Relational operators
Operator Meaning
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
== Equal to
!= Not equal to
Boolean expressions using relational operators
Expression Meaning
x>y Greater than
x<y Less than
x >= y Greater than or equal to
x <= y Less than or equal to
x == y Equal to
x != y Not equal to
Boolean expressions using relational operators
Expression Meaning
x >y Greater than
x<y Less than
x >= y Greater than or equal to
x <= y Less than or equal to
x == y Equal to
x != y Not equal to
Example 1
If sales > 50000 Then
Set bonus = 500.0
Set commissionRate = 0.12
Display "You've met your sales quota!"
End If
Example 1 (Continue)
True
sales >
50000
Set
False commissionRate =
0.15
Display “You’ve
met your sales
quota!”
Exercise
Alpha teaches a science class and her students are required to
take three tests. He wants to write a program that his students
can use to calculate their average test score.
He also wants the program to congratulate the student
enthusiastically if the average is greater than 95.
Algorithm:
1. Get the first test score.
2. Get the second test score.
3. Get the third test score.
4. Calculate the average.
5. Display the average.
6. If the average is greater than 95, congratulate the user.
Pseudocode:
Declare Real test1, test2, test3, average
// Get test score
Display “Enter the score for test #1.”
Input test1
Display “Enter the score for test #2.”
Input test2
Display “Enter the score for test #3.”
Input test3
Set average = (test1 + test2 + test3) / 3 // Calculate the
average
Display “The average is “, average // Display the average.
If average > 95 Then
Display "Congratulations! Great average!"
End If
Flowchart
A A
Start
A
A
Dual Alternative Decision Structures
A dual alternative decision structure will execute one group
of statements if its Boolean expression is true, or another
group if its Boolean expression is false.
True
False
Temperat
ure < 40
Display "per year to Display "You must have Display "You qualify
qualify." been on your current" for the loan."
End
Testing a Series of Conditions with If-Then-Else If
Statement
Dr. Vandy teaches a programming class and uses the following
10 point grading scale for all of his exams:
Test Score Grade
90 and above A
80–89 B
70–79 C
60–69 D
Below 60 F
He has asked you to write a program that will allow a student
to enter a test score and then display the grade for that score.
Declare Real score
Display "Enter your test score."
Input score
If score < 60 Then
Display "Your grade is F."
Else If score < 70 Then
Display "Your grade is D."
Else If score < 80 Then
Display "Your grade is C."
Else If score < 90 Then
Display "Your grade is B."
Else
Display "Your grade is A."
End If
The Case Structures
The case structure is a multiple alternative decision structure.
It allows you to test the value of a variable or an expression
and then use that value to determine which statement or set
of statements to execute.
mon
th
1 2 3 Default
Display Display
"January" Display “March" Display “April"
“February"
Declare Integer month
Display “Enter the month"
Input month
Select month
Case 1:
Display “January"
Case 2:
Display “February"
Case 3:
Display “March”
Default:
Display “April”
End Select
Logical Operators
The logical AND operator and the logical OR operator allow you to
connect multiple Boolean expressions to create a compound
expression. The logical NOT operator reverses the truth of a
Boolean expression.
AND: The AND operator connects two Boolean expressions into
one compound expression. Both subexpressions must be true for
the compound expression to be true.
OR: The OR operator connects two Boolean expressions into one
compound expression. One or both subexpressions must be true
for the compound expression to be true. It is only necessary for
one of the subexpressions to be true, and it does not matter which.
NOT: The NOT operator is a unary operator, meaning it works with
only one operand. The operand must be a Boolean expression. The
NOT operator reverses the truth of its operand. If it is applied to an
expression that is true, the operator returns false. If it is applied to
an expression that is false, the operator returns true.
Compound Boolean expressions using logical
operators
Expression Meaning
x > y AND a < b Is x greater than y AND is a less than
b?
x == y OR x == z Is x equal to y OR is x equal to z?
NOT (x > y) Is the expression x > y NOT true?
The AND Operator
Quantity Discount
10 – 19 20%
20 – 49 30%
50 – 99 40%
100 or more 50%
Design a program that asks the user to enter the number of packages
purchased. The program should then display the amount of the
discount (if any) and the total amount of the purchase after the
discount.