Lecture 12 If Statement
Lecture 12 If Statement
Lecture 12 If Statement
If Statement
Alex Kuhudzai
alexkuhudzai@gmail.com
Cape Peninsula University of Technology,
Cape Town, South Africa
The If Statement
The if statement is used to create a decision structure, which allows a program to
have more than one path of execution. The if statement causes one or more
statements to execute only when a Boolean expression is true.
A control structure is a logical design that controls the order in which a set of
statements execute. A sequence structure is a set of statements that execute in the
order in which they appear. For example, the following code is a sequence
structure because the statements execute from top to bottom:
The If Statement
The if statement is used to create a decision structure, which allows a program to
have more than one path of execution. The if statement causes one or more
statements to execute only when a Boolean expression is true.
A control structure is a logical design that controls the order in which a set of
statements execute. So far in this book, we have used only the simplest type of
control structure: the sequence structure. A sequence structure is a set of
statements that execute in the order in which they appear. For example, the
following code is a sequence structure because the statements execute from top to
bottom:
The If Statement
Although the sequence structure is heavily used in programming, it cannot handle every
type of task. This is because some problems simply cannot be solved by performing a set
of ordered steps, one after the other. For example, consider a pay calculating program that
determines whether an employee has worked overtime. If the employee has worked more
than 40 hours, he or she gets paid extra for all the hours over 40. Otherwise, the overtime
calculation should be skipped. Programs like this require a different type of control
structure: one that can execute a set of statements only under certain circumstances.
This can be accomplished with a decision structure. (Decision structures are also known as
selection structures.) In a decision structure’s simplest form, a specific action is performed
only if a certain condition exists. If the condition does not exist, the action is not
performed. The flowchart shown on the next slide shows how the logic of an everyday
decision can be diagrammed as a decision structure. The diamond symbol represents a
true/false condition. If the condition is true, we follow one path, which leads to an action
being performed. If the condition is false, we follow another path, which skips the action.
The If Statement
In the flowchart, the diamond symbol indicates some condition that must be tested. In this
case, we are determining whether the condition Cold outside is true or false. If this condition
is true, the action Wear a coat is performed. If the condition is false, the action is skipped.
The action is conditionally executed because it is performed only when a certain condition
is true.
The If Statement
Let’s look at the following example of the if statement:
if sales > 50000:
bonus = 500.0
This statement uses the > operator to determine whether sales is greater than 50,000. If
the expression sales > 50000 is true, the variable bonus is assigned 500.0. If the
expression is false, however, the assignment statement is skipped. The figure on the
next slide shows a flowchart for this section of code.
The If Statement
The If Statement
The following code uses the == operator to determine whether two values are equal. The
expression balance == 0 will be true if the balance variable is assigned 0. Otherwise
the expression will be false.
if balance == 0:
# Statements appearing here will
# be executed only if balance is
# equal to 0.
The following code uses the != operator to determine whether two values are not equal.
The expression choice != 5 will be true if the choice variable does not reference the value
5. Otherwise, the expression will be false.
if choice != 5:
# Statements appearing here will
# be executed only if choice is
# not equal to 5.
An Example
Kathryn teaches a science class and her students are required to take three tests.
She wants to write a program that her students can use to calculate their average
test score. She also wants the program to congratulate the student enthusiastically
if the average is greater than 95.
Lets put together the pseudocode