Flow of Control
Types of statements
• In Python, statements are of 3 types-
» Empty Statements
pass
» Simple Statements (Single Statement)
• name=input (“Enter your Name “)
• print(name) etc.
» Compound Statements
• if else
• for
• while
Statement Flow Control
• In a program, statements executes in sequential, selective
or iterative manner.
Sequential Selective Iterative
A program illustrating sequential control.
Selectionif Statement
Statement : The ‘if’ statement
• Decision making in Python is achieved using the ‘if’
statement. Following is the syntax:
if <condition>:
statement(s)
if-else Statement
• If out of two statements, it is required to select one
statement for processing on the basis of a condition, if-
else statement is to be used. Its syntax is-
if <condition>:
statement(s) when condition is true
else:
statement(s) when condition is false
like -
if-elif Statements
• If out of multiple statements, it is required to select one
statement for processing on the basis of a condition, if-
elif statement is to be used. Its syntax is-
if <condition1>: like -
statement(s) when condition1 is true
elif <condition2>:
statement(s) when condition2 is true
elif <condition3>:
statement(s) when condition3 is
true
else
Nested If -else
Loop/ Repetition/ Iteration
These control structures are used for repeated execution of
statement(s) on the basis of a condition. Loop has 3 main
components-
1. Start (initialization of loop)
2. Step (moving forward in loop )
3. Stop (ending of loop)
Python has following loops-
– for loop
– while loop
range () Function
• InPython, an important function is range( ).
its syntax is-
range ( <lower limit>,<upper limit>)
If we write - range (0,5 )
Then a list will be created with the values [0,1,2,3,4]
i.e. from lower limit to the value one less than ending limit.
range (0,10,2) will have the list
[0,2,4,6,8]. range (5,0,-1) will have the list
[5,4,3,2,1].
in and not in operator
• in operator-
3 in [1,2,3,4] will return True.
5 in [1,2,3,4] will return False.
– not in operator-
5 not in [1,2,3,4] will return True.
Table of a number by For loop
Syntax of For Loop
for <var> in <sequence>:
<statements to repeat> Output
Table of a number by while loop
Syntax of While Loop
While <LogicalExpression>:
<loop body with increment
or decrement>
Output
Start
Stop
Step
Jump Statements
break Statement
while <test-condition>: for <var> in <sequence>:
statement1 statement1
if <condition>: if <condition>:
break break
statement2 statement2
statement3 statement3
Statement4 Statement4
statement5 statement5
Jump Statements
break Statement
Output Output
Jump Statements
continue Statement
while <test-condition>: for <var> in <sequence>:
statement1 statement1
if <condition>: if <condition>:
continue continue
statement2 statement2
statement3 statement3
Statement4 Statement4
statement5 statement5
Jump Statements
continue Statement
Output of both the
programs
Nested Loop
OUTPUT
Assignments
1.WAP to find greatest among three numbers.
2.WAP to print the result based on marks
entered of a student.
3.WAP to print counting to n.
4.WAP to print even numbers up to n.
5.WAP to print odd numbers up to n.
6.WAP to print Fibonacci series.
7.WAP to calculate xn.
8.WAP to calculate n!.
9.WAP to print different patterns.