6 - Lectures Note Week7 Fundamentals of Programming (3) Python Conditional and Iterative Statements
6 - Lectures Note Week7 Fundamentals of Programming (3) Python Conditional and Iterative Statements
Solving Tool
Topic 5:
Fundamentals of Programming (3)
Lecture6 Week7:
Lecturer
Dr. Ufuoma C. Ogude
2
3
4
5
CONDITIONAL AND ITERATIVE
STATEMENTS
SEQUENCE
7
SELECTION
8
LOOP
9
SUBPROGRAM
10
CONTROL FLOW
A program’s control flow is the order in which
the program’s code executes.
The control flow of a python program is
regulated by conditional statements, loops and
functions calls.
While loop:
In python, while loops are used to execute a
block of statements repeatedly until a given
condition is satisfied.
Then, the expression is checked again and, if
it is still true, the body is executed again. 14
SEQUENCE SELECTION ITERATION
1. SEQUENCE
2. SELECTION
3. ITERATION OR LOOPING
Statement 1
Statement 2
Statement 3
……..
……..
……..
1. SEQUENCE – FLOW CHART
1. SEQUENCE – FLOW CHART
Statement 1
Statement 2
Statement 3
SEQUENCE
1. SEQUENCE - PROGRAM
1. SEQUENCE - PROGRAM
SELECTION
2. SELECTION
: Colon Must
if first condition:
first body
elif second condition:
second body
elif third condition:
third body
else:
fourth body
CONDITIONAL CONSTRUCT – if else STATEMENT
FLOW CHART
CONDITIONAL CONSTRUCT – if else STATEMENT
FLOW CHART
False
Condition ? Statement 1 Statement 2
Main True
Body
Statement 1
else
Statement 2 body
CONDITIONAL CONSTRUCT – if else STATEMENT
EXAMPLE - PROGRAM
EXAMPLES – if STATEMENT
else is missing,
it is an optional
statement
OUT PUT
CONDITIONAL CONSTRUCT
: Colon Must
else is
used
OUT PUT
CONDITIONAL CONSTRUCT
READ AS
18 is less
than age
and
18 is less
than 60
OUTPUT
PROGRAM LIST ON if CONTSTUCT
PROGRAM LIST ON if CONTSTUCT
AVERAGE PROGRAMS
5. Write a PYTHON program to evaluate the
student performance
If % is >=90 then Excellent performance
If % is >=80 then Very Good performance
If % is >=70 then Good performance
If % is >=60 then average performance
else Poor performance.
6. Write a PYTHON program to find largest of
three numbers.
7. Write a PYTHON program to find smallest of
three numbers
PROGRAM LIST ON if CONTSTUCT
***
3. ITERATION OR LOOPING
ITERATION
3. ITERATION OR LOOPING
while loop
for loop
while loop
while loop
FLOW CHART
while loop
while loop – Programming example
while loop - programs
# Natural Numbers generation
OUTPUT
while loop - programs
# Calculating Sum of Natural Numbers
OUTPUT
while loop - programs
#Generating Fibonacci numbers
while loop - programs
#Generating Fibonacci numbers
OUTPUT
while loop - Programs
AVERAGE PROGRAMS
OUTPUT
for LOOP
range KEYWORD
for LOOP - range KEYWORD
x = range(3, 6)
for n in range(3,6):
OR for n in x:
print(n)
print(n)
for LOOP - range KEYWORD
OUTPUT
for LOOP - range KEYWORD
#Generating even numbers
OUTPUT
for LOOP – len() FUNCTION
for LOOP - range KEYWORD
# print string character by character
OUTPUT
else statement in loop
else statement in loop
AVERAGE PROGRAMS
6. Write a PYTHON program to compute the cosine
series
cos(x) = 1 – x2 / 2! + x4 / 4! – x6 / 6! + … xn / n!
1. break STATEMENT
2. continue STATEMENT
4. BRANCHING OR JUMPING STATEMENTS
1. break STATEMENT
Loop
Condition ? break
causes
jump
True
Statement 1
break
1. break STATEMENT
OUT PUT
2. continue STATEMENT
Loop False
Condition ?
True
Statement 1
Statements
ignored or continue
skipped
continue
Statement n
causes
jump
2. continue STATEMENT
break continue
Difference Between break and continue
BREAK CONTINUE
It terminates the execution It terminates only the current
of remaining iteration of iteration of the loop.
the loop.
'break' resumes the control 'continue' resumes the control
of the program to the end of the program to the next
of loop enclosing that iteration of that loop enclosing
'break'. 'continue'.
It causes early termination It causes early execution of the
of loop. next iteration.
'break' stops the 'continue' do not stops the
continuation of loop. continuation of loop, it only
stops the current iteration.
CLASS TEST
CLASS TEST