Conditional Statement Handout
Conditional Statement Handout
CONDITIONAL STATEMENTS
Explanation
Line 2 – This instruction checks thevalue
When a program is executed, each instruction
stored in the variable Time toseeif it contains
is processed in the sequence listed in the
the value 11.
program, unless specific instructions direct it
to deviate and select other instructions.
Line 3 – The message “Ringthebell”will be
displayed to the user if the result obtainedfrom
THE IF-STATEMENT
Line 2 is true (that is the variableTime
The if-statement is a conditional statement and
contains the value 11). If the result obtained
it allows deviation and selection to take place.
from Line 2 is not true nothingwill be
Read Age (Line1)
displayed.
If Age > 35 then (Line2)
Choosing the correct if-statement to
solve the problem
Line 4 – This instruction terminates theif-then
Else (Line4)
statement.
A number of if-statements are available for use
in programming. Each type of if-statement
(Line5)
Print “Young person” Endif (Line6)
THE IF-THEN-ELSESTATEMENT
will perform differently; so it is very important
When two options are available andaselection
Read Time (Line 1)
to know what results you are typing to
accomplish. must be made, use the if-then-elsestatement. Layout
Endif (Line 4)
THE IF-THEN STATEMENT
If <condition> then
When one option is available and a selection
<one or more instructions whichwill be
may or may not be made, the if-then statement
is used. Layout carried out if the conditionis true>Else
<one or more instructions whichwill be carried
out if the conditionis false>
If <condition> then
Endif
<one or more instructions which will be carried out if the
condition is true>
Endif ageisgreaterthan 35, output “Old person”
Practice Question Input the age of a person. If the otherwiseoutput
The instructions between the if-then and endif
“Young person”.
are executed only if the condition is true. If
the comparison is false, the instructions
Solution
between the if-then and endif are ignored.
Practice Question
Print “Old person”(Line3)
Read the time. If the time is 11:00, output
“Ring the bell”.
Solution
20 FPowell jesusaves.fp@gmail.com
Read Stand (Line1)
Read Spectators (Line2)
If Stand = “A” then (Line3)
Else (Line5)
If Stand = “B” then (Line6)
Else (Line8)
If Stand = “C” then (Line9)
Else (Line11)
If Stand = “D” then (Line12)
Endif (Line14)
Endif (Line15)
Endif (Line16)
Endif (Line17)
Print “Stand”, Stand (Line18)
Explanation
Solution
Line 3 – The message “Old person” will be
displayed to the user if the result obtained from Line 2 is true (that is the value stored in the
variable Age is greater than 35).
Revenue = Spectators * 2.00(Line4)
THE IF-THEN-ELSE-IF STATEMENT When two or more options are available and a
selection may or may not be made, use the if
then-else-if statement.
Print “Revenue”, Revenue (Line19)
Layout
If <condition> then
<one or more instructions>
Else
If <condition> then
<one or more instructions>
Else
If <condition> then
<one or more instructions>
Endif
Endif
Endif
Practice Question
A stadium has four stands A, B, C, D. The
admission fee for stand A is 2.00, stand B is 2.50, stand C is 4.00 and stand D is 5.00. Read a stand and
the number of spectators in
the stand. Calculate and print the revenue for
the stand.
21 FPowell jesusaves.fp@gmail.com