Lecture Module 3 - Conditional Program Flow
Lecture Module 3 - Conditional Program Flow
➢ Flowchart
➢ If
➢ if else
➢ elif
Prepare a flowchart to calculate the area of triangle with the side a, b, and c, them write
the program following the designed flowchart.
Area= √ s(s−a)(s−b)(s−c)
(a+b+c)
s=
2
Start
Implement Heron
Calculate
Formula
S, then Area
s, Area
Avoid window
to disappear
Stop
Try on your own and run it
Try on your own and run it, you will learn additional commands
\n
print()
%0.2f
Import math
math.sqrt
Import math as m
m.sqrt
If condition
Decision making (if condition) is used to anticipate conditions that occur during the course of
the program and determine what actions will be taken according to the conditions.
Read
number
true
Number > 7
false
display
Correct number
c a
b
Try these data:
a, b, c 4, 2, 1 (this is not a triangle)
can form a triangle 5, 6, 7 (this is a triangle)
if
Learn from the results
a+b>c
b+c>a
c+a>b
c a
b
Try these data:
a, b, c 4, 2, 1 (this is not a triangle)
can form a triangle 5, 6, 7 (this is a triangle)
if
Learn from the results
a+b>c
b+c>a
c+a>b
Read
a,b,c
Check if true
not triangle
false
display
Not Triangle
display
Stop
Well done,
This is triangle
if else Conditions
Decision making (if else condition) is not only used to determine what action will be taken
according to the condition, but also used to determine what action will be taken / executed if the
condition does not fit.
Read
number
true
Number > 9
false
display display
Incorrect number Correct number
In this program you also learn how to break string if you want to write to the next line
Lecture Module 3 Conditional Program Flow
elif condition
elif Condition
Decision making (if elif condition) is a continuation/branching logic from the "if condition". With
elif we can create program code that will select several possibilities that can occur. Almost the
same as the "else" condition, the difference is that the "elif" condition can be many and not just
one.
Read
Nilai Test (1-10)
if true
nilai > 9
false
display
Cemerlang
elif true
nilai > 5
false display
Baik Sekali
elif true
1<= nilai <=5
display
Tidak lulus
display
Konversi selesai
Now you have to improve the program in Example 3-4 after checking that the data is a
valid triangle, you have to classify the name of the triangle by the sides.