Python.Week 3
Python.Week 3
if Boolean_Expression :
statement (s)
if Boolean_Expression :
statement_blk_1
else:
statement_blk_2
The if…elif…else Decision Control Statement
The if…elif…else is also called as multi-way decision control statement.
The syntax for if…elif…else statement is,
if Boolean_Expression_1 :
statement_blk_1
elif Boolean_Expression_2 :
statement_blk_2
elif Boolean_Expression_3 :
statement_blk_3 : : :
else :
statement_blk_last
Nested if Statement
An if statement that contains another if statement either in its if block or else block is
called a Nested if statement.
The syntax of the nested if statement is,
if Boolean_Expression_1 :
if Boolean_Expression_2 :
statement_blk_1
else :
statement_blk_2
else :
statement_blk_3
LOOPS IN PYTHON
Loops:
Loop is a control structure that allows the execution of a statement or block of
statements multiple times until a loop termination condition is met.
Loop control flow statements are also called Repetition or Iteration statements
Types of Loops :
Loops
while Boolean_Expression:
Statement(s)
Flow of Execution for a while statement
1. Evaluate the condition or the boolean expression , yielding true or false.
2. If the boolean expression is false,exit the while statement and then continue
execution at the next statement.
3. If the Boolean expression is true execute the body and then go back.
Example :
Program that counts down from five and then says “Blastoff!”
For Loop
A for loop in python is used to iterate over a sequence.
Syntax of For Loop
Outer_loop Expression:
Inner_loop Expression:
Statement inside inner_loop
Statement inside Outer_loop
Example
Jump statements in Python
● The break statement in Python is used to terminate the loop or statement in
which it is present.
● Whenever the break statement is encountered, the execution control
immediately jumps to the first instruction following the loop.
● To pass control to the next iteration without exiting the loop, use the continue
statement.
● Both continue and break statements can be used in while and for loops.
Break Example
Continue example
Pass statement