MEN3221221
Basic Programming in Python
Lecture Module 2: Conditional Program Flow
Prof. Dr. Ir. Supriyono
Prof. Dr. Ir. Waluyo Adi Siswanto
Learning Outcomes
➢ Use flowchart with standard symbols to design program
➢ Implement if, if else, and elif conditions in the program
➢ Apply conditional in real application
Lecture Module 3 Conditional Program Flow
Topics
➢ Flowchart
➢ If
➢ if else
➢ elif
Lecture Module 3 Conditional Program Flow
Flowchart
●
A flowchart is a method of diagramming the logic
of an algorithm using a standardized set of
symbols to indicate the logical flow of the
program.
●
The logical flow of the algorithm is from top to
bottom, and alternative paths are indicated by flow
lines with arrowheads to indicate the direction of
the process.
●
A flowchart should have one start and one or
more stops.
●
Flow line may cross, but merging of two or more
computational paths at a point in a program
indicated by means of the connector symbol
(circle)
Lecture Module 3 Conditional Program Flow
Data are read or results are printed out
Terminal
either via batch processing or at terminal
Input / Output Data are read or results are printed out
either via batch processing or at terminal
A collection of statements that performs
Process
some sort of computation or operation
Decision The branching to alternate paths on the
basis of requirement condition
A connector, a point where several flow
lines merged
Annotation A box containing explanatory remarks or
comments
Lecture Module 3 Conditional Program Flow
Example 3-1
Prepare a flowchart to calculate the area of triangle with the side a, b, and c, them write
the program following the designed flowchart.
Using Heron’s formula
( https://en.wikipedia.org/wiki/Heron%27s_formula)
Area= √ s(s−a)(s−b)(s−c)
(a+b+c)
s=
2
Lecture Module 3 Conditional Program Flow
The first thing is make sure your program
running correctly
Start
Read data triangle
a, b, c
Implement Heron
Calculate
Formula
S, then Area
s, Area
Display nicely results
Area
Avoid window
to disappear
Stop
Try on your own and run it
Lecture Module 3 Conditional Program Flow
Once running you can improve to appear better
for users
Try on your own and run it, you will learn additional commands
\n
print()
%0.2f
and to print formatted variable area by using % written as %area
Lecture Module 3 Conditional Program Flow
Try on your own and run it, you will learn additional commands
Using build in function sqrt
Import math
math.sqrt
Lecture Module 3 Conditional Program Flow
Try on your own and run it, you will learn additional commands
Using build in function sqrt . Rename the module math for example as m
Import math as m
m.sqrt
Other functions of module math: https://docs.python.org/release/3.11.5/library/math.html#module-math
Lecture Module 3 Conditional Program Flow
if condition
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
Try to enter 8 then try 6
display
we are here Learn from the results
Lecture Module 3 Conditional Program Flow
Example 3-2
Write a program to check if data
sides a, b, c can form a triangle
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
Lecture Module 3 Conditional Program Flow
Example 3-3
Write a program to check if data sides a, b, c can form a triangle.
If not triangle stop the program. Use raise SystemExit() to stop the program.
Draw the flowchart before writing the codes.
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
Lecture Module 3 Conditional Program Flow
Start
Read
a,b,c
Check if true
not triangle
false
display
Not Triangle
display
Stop
Well done,
This is triangle
You can check the data
Stop triangle is valid or not.
Stop the program if not
valid
Lecture Module 3 Conditional Program Flow
Example 3-4
Now implement the
checking triangle to your
triangle calculation
program.
Check the data a, b, and
c first. Stop the program
if not a triangle, and
continue the calculation if
a triangle.
Yes…., my program looks nice
Lecture Module 3 Conditional Program Flow
if else condition
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
Try to enter 9.1 then try 7
display
we are here Learn flow from the results
Lecture Module 3 Conditional Program Flow
Example 3-5
Rewrite the codes in
example 3-4. You have
to use if else structure.
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
Lecture Module 3 Conditional Program Flow
elif condition
Lecture Module 3 Conditional Program Flow
Example 3-6
Write a program to convert examination result to grade, by using conditional structure
- the examination results from 0 to 100.0
- the conversion process continues if the entry is valid score in the range of 0-100 only
- the conversion table
Result score Grade Scale
85 <= score <= 100 A
70 <= score < 85 B
50 <= score < 70 C
40 <= score < 50 D
0 <= score < 40 E
Lecture Module 3 Conditional Program Flow
Lecture Module 3 Conditional Program Flow
Example 3-7
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.
Lecture Module 3 Conditional Program Flow
Lecture Module 3 Conditional Program Flow
Lecture Module 3 Conditional Program Flow