0% found this document useful (0 votes)
4 views100 pages

6 - Lectures Note Week7 Fundamentals of Programming (3) Python Conditional and Iterative Statements

The document outlines a lecture on programming fundamentals, focusing on control flow in Python, including sequences, selection statements, and iteration structures. It explains the types of control structures such as sequential, selection, and iteration, detailing how they are implemented in Python with examples. Additionally, it provides learning outcomes for students, emphasizing their ability to analyze problems and write code using decision structures and loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views100 pages

6 - Lectures Note Week7 Fundamentals of Programming (3) Python Conditional and Iterative Statements

The document outlines a lecture on programming fundamentals, focusing on control flow in Python, including sequences, selection statements, and iteration structures. It explains the types of control structures such as sequential, selection, and iteration, detailing how they are implemented in Python with examples. Additionally, it provides learning outcomes for students, emphasizing their ability to analyze problems and write code using decision structures and loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 100

CSC120: Computer as a Problem-

Solving Tool
Topic 5:
Fundamentals of Programming (3)
Lecture6 Week7:
Lecturer
Dr. Ufuoma C. Ogude

Department of Computer Sciences


University of Lagos
1
Outlines of the Course
Wk2-7
 Concept of Problem solving
 Introduction to programming
 Concept & properties of Algorithm
 Fundamentals of programming (1): variable/variable
naming convention/data types in Python
 Fundamentals of programming (2): operators and
expressions
 Fundamentals of programming (3): control structures

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.

 Python has three types of control structures


 Sequential – default mode
 Selection – used for decisions and
branching 11
CONTROL FLOW
 Sequential:
Are sets of statements whose
execution process happens in a
sequence.
Example:
a = 20
b = 10
c=a–c
print(“subtraction is : ” , c)
12
CONTROL FLOW
 Selection/Decision Control
Statements:
The selection statement allows a
program to test several conditions
and execute instructions based on
which condition is true.

 Some decision control statements


are:
 Simple If
 If – else 13
CONTROL FLOW
Repetition: Repetition Statements are used
to repent a group (block) of programming
instruction. We have two loops/repetitive
statements:
 For loop:
It is used to iterate over a sequence that is
either a list type, dictionary or a set

 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

CONDITIONAL AND ITERATIVE


STATEMENTS
CONDITIONAL AND ITERATIVE
STATEMENTS
LEARNING OUTCOMES
LEARNING OUTCOMES

After studying this lesson, students will be able to:

Understand the concept and usage of selection


and iteration statements.

Know various types of loops available in Python.

Analyze the problem, decide and evaluate


conditions.
LEARNING OUTCOMES

After studying this lesson, students will be able to:

Will be able to analyze and decide for an


appropriate combination of constructs.

Write code that employ decision structures,


including those that employ sequences of decision
and nested decisions.

Design simple applications having iterative


nature.
INTRODUCTION
INTRODUCTION
 Programs are written for the solution to the real world
problems.
 A language should have the ability to control the flow
of execution so that at different intervals different
statements can be executed.
 Structured programming is a paradigm aims at
controlling the flow of execution of statements in a
program by using control structures.
A language which supports the control structures is called
as structured programming language
TYPES OF CONTROL STRUCTURES
TYPES OF CONTROL STRUCTURES

A Structured programming is an important


feature of a programming language which
comprises following logical structure:

1. SEQUENCE

2. SELECTION

3. ITERATION OR LOOPING

4. BRANCHING OR JUMPING STATEMENTS


1. SEQUENCE

Sequence is the default control structure;


instructions are executed one after another.

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

Sequence is the default control structure;


instructions are executed one after another.
# This program adds two numbers
def sum_of_two_no():
num1 = 1.5
num2 = 6.3
sum = float(num1) + float(num2)
print('The sum is =‘, sum)
sum_of_two_no():
2. SELECTION

SELECTION
2. SELECTION

A selection statement causes the


program control to be transferred to a
specific flow based upon whether a certain
condition is true or not.
CONDITIONAL CONSTRUCT – if else STATEMENT
CONDITIONAL CONSTRUCT – if else STATEMENT

Conditional constructs (also known as if


statements) provide a way to execute a chosen
block of code based on the run-time evaluation of
one or more Boolean expressions. In Python, the
most general form of a conditional is written as
follows:

Contd.. Next Slide


CONDITIONAL CONSTRUCT – if else STATEMENT

: 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

Each condition is a Boolean expression, and


each body contains one or more commands that
are to be executed conditionally.

If the first condition succeeds, the first body will


be executed; no other conditions or bodies are
evaluated in that case.
CONDITIONAL CONSTRUCT – if else STATEMENT

If the first condition fails, then the process


continues in similar manner with the evaluation
of the second condition. The execution of this
overall construct will cause precisely one of the
bodies to be executed.

There may be any number of elif clauses


(including zero), and
 The final else clause is optional.
CONDITIONAL CONSTRUCT – if else STATEMENT

EXAMPLE - PROGRAM
EXAMPLES – if STATEMENT

else is missing,
it is an optional
statement

OUT PUT
CONDITIONAL CONSTRUCT

EXAMPLE – if else STATEMENT


EXAMPLE – if else STATEMENT

: Colon Must

else is
used

OUT PUT
CONDITIONAL CONSTRUCT

EXAMPLES – if elif STATEMENT


EXAMPLES – if elif STATEMENT

READ AS
18 is less
than age
and
18 is less
than 60

OUTPUT
PROGRAM LIST ON if CONTSTUCT
PROGRAM LIST ON if CONTSTUCT

BELOW AVERAGE PROGRAMS


1. Write a PYTHON program that reads a value of n
and check the number is zero or non zero value.
2. Write a PYTHON program to find a largest of
two numbers.
3. Write a PYTHON program that reads the
number and check the no is positive or negative.
4. Write a PYTHON program to check entered
character is vowel or consonant.
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

ABOVE AVERAGE PROGRAMS


8.Write a PYTHON program to check weather
number is even or odd.
9.Write a PYTHON program to check a year for
leap year.
10. A company insures its drivers in the following
cases:
- If the driver is married.
- If the driver is unmarried, male and above 30
years of age.
- If the driver is unmarried, female and above 25
years of age.
PROGRAM LIST ON if CONTSTUCT

ABOVE AVERAGE PROGRAMS

In all the other cases, the driver is not insured.


If the marital status, sex and age of the driver are
the inputs,

Write a PYTHON program to determine


whether the driver is insured or not

***
3. ITERATION OR LOOPING

ITERATION
3. ITERATION OR LOOPING

What is loop or iteration?


Loops can execute a block of code number
of times until a certain condition is met.
OR
The iteration statement allows instructions to
be executed until a certain condition is to be
fulfilled.
The iteration statements are also called as
loops or Looping statements.
3. ITERATION OR LOOPING

Python provides two kinds of loops


& they are,

while loop

for loop
while loop
while loop

A while loop allows general repetition


based upon the repeated testing of a Boolean
condition
The syntax for a while loop in Python is as
follows:
while condition: : Colon Must
body
Where, loop body contain the single
statement or set of statements (compound
statement) or an empty statement.
Contd..
while loop

The loop iterates while the expression


evaluates to true, when expression becomes
false the loop terminates.

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

Class work / Home Work


while loop - programs

BELOW AVERAGE PROGRAMS

1. Write a PYTHON program to print the natural


numbers up to n
2. Write a PYTHON program to print even
numbers up to n
3. Write a PYTHON program to print odd numbers
up to n
4. Write a PYTHON program to print sum of
natural numbers up to n
while loop - programs

AVERAGE PROGRAMS

5. Write a PYTHON program to print sum of odd


numbers up to n
6. Write a PYTHON program to print sum of even
numbers up to n
7. Write a PYTHON program to print natural
numbers up to n in reverse order.
8. Write a PYTHON program to print Fibonacci
series up to n
9. Write a PYTHON program find a factorial of
given number
while loop - programs

ABOVE AVERAGE & HOTS PROGRAMS

10. Write a PYTHON program to check the


entered number is prime or not
11. Write a PYTHON program to find the sum of
digits of given number
12. Write a PYTHON program to check the
entered number is palindrome or not
13. Write a PYTHON program to reverse the given
number.
while loop - programs

ABOVE AVERAGE & HOTS PROGRAMS

14. Write a PYTHON program to print the


multiplication table
15. Write a PYTHON program to print the largest
of n numbers
16. Write a PYTHON program to print smallest of
n numbers
for LOOP
for LOOP

Python’s for-loop syntax is a more


convenient alternative to a while loop when
iterating through a series of elements. The for-
loop syntax can be used on any type of iterable
structure, such as a list, tuple str, set, dict, or
file
Syntax or general format of for loop is,

for element in iterable:


body
for LOOP

Python’s for-loop syntax is a more


convenient alternative to a while loop when
iterating through a series of elements. The for-
loop syntax can be used on any type of iterable
structure, such as a list, tuple str, set, dict, or
file
Syntax or general format of for loop is,

for element in iterable:


body
for LOOP

Till the list


exhaust for loop
will continue to
execute.

OUTPUT
for LOOP

range KEYWORD
for LOOP - range KEYWORD

The range() function returns a


sequence of numbers, starting from 0 by
default, and increments by 1 (by default),
and ends at a specified number.

range(start, stop, step)

x = range(3, 6)
for n in range(3,6):
OR for n in x:
print(n)
print(n)
for LOOP - range KEYWORD

#Generating series of numbers

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

else can be used in for and while loops the


else body will be executed as and when the
loop’s conditional expression evaluates to false
OUTPUT
for loop - Programs

Class work / Home Work


for loop – Programs - Class work / Home Work

BELLOW AVERAGE PROGRAMS


1. Write a PYTHON program to print the natural
numbers up to n
2. Write a PYTHON program to print even numbers
up to n
3. Write a PYTHON program to print odd numbers
up to n
4. Write a PYTHON program that prints 1248
16 32 … n2
5. Write a PYTHON program to sum the given
sequence
1 + 1/ 1! + 1/ 2! + 1/3! + …. + 1/n!
for loop – Programs - Class work / Home Work

AVERAGE PROGRAMS
6. Write a PYTHON program to compute the cosine
series
cos(x) = 1 – x2 / 2! + x4 / 4! – x6 / 6! + … xn / n!

7. Write a short PYTHON program to check weather


the square root of number is prime or not.
8. Write a PYTHON program to produce following
design
ABC
ABC
ABC
for loop – Programs - Class work / Home Work

ABOVE AVERAGE PROGRAMS

9. Write a PYTHON program to produce following


design
A
AB
ABC
ABCD
ABCDE
If user enters n value as 5
for loop – Programs - Class work / Home Work

ABOVE AVERAGE PROGRAMS

10. Write a PYTHON program to produce following


design
ABCDE
ABCD
ABC
AB
A
(If user enters n value as 5)
for loop – Programs - Class work / Home Work

ABOVE AVERAGE PROGRAMS

11. Write a PYTHON program to produce


following design
1
12
123
1234
12345
If user enters n value as 5
for loop – Programs - Class work / Home Work

ABOVE AVERAGE PROGRAMS

12. Write a PYTHON program to produce


following design
1
22
333
4444
55555
If user enters n value as 5
4. BRANCHING OR JUMPING STATEMENTS
4. BRANCHING OR JUMPING STATEMENTS

Python has an unconditional branching


statements and they are,

1. break STATEMENT

2. continue STATEMENT
4. BRANCHING OR JUMPING STATEMENTS

1. break STATEMENT

Break can be used to unconditionally


jump out of the loop. It terminates the
execution of the loop. Break can be used in
while loop and for loop. Break is mostly
required, when because of some external
condition, we need to exit from a loop.
1. break STATEMENT

Loop
Condition ? break
causes
jump
True

Statement 1

break
1. break STATEMENT

OUT PUT
2. continue STATEMENT

The continue statement in


Python returns the control to the
beginning of the while loop. The continue
statement rejects all the
remaining statements in the current
iteration of the loop and moves the
control back to the top of the loop.
The continue statement can be used in
both while and for loops.
2. continue STATEMENT

Loop False
Condition ?

True
Statement 1

Statements
ignored or continue
skipped
continue
Statement n
causes
jump
2. continue STATEMENT

when i value becomes 2 the print statement gets


skipped, continue statement goes for next iteration,
hence in the out put 2 is not printed
pass STATEMENT
pass STATEMENT

The pass statement in Python is used when a


statement is required syntactically but you do not
want any command or code to execute.
The pass statement is a null operation; nothing
happens when it executes.
The pass is also useful in places where your
code will eventually go, but has not been written
yet (e.g., in stubs for example):
pass STATEMENT

pass in loop has


pass in loop no output
Difference Between break and continue

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

Time: 40 Min Max Marks: 20

1. Explain the types control structures 05


2. Write a program to check number is prime or
not 05

3. Write a program to check the entered number


is Armstrong number or not 05

4. Differentiate between break and continue 05


Thank You

You might also like