0% found this document useful (0 votes)
5 views

Python.Week 3

The document outlines control flow statements in Python, which include sequential, decision, and loop statements. It details various decision structures such as if, if...else, and nested if statements, as well as loop types like while and for loops. Additionally, it discusses jump statements like break, continue, and return, explaining their functions within control flow.

Uploaded by

bichumbk08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Python.Week 3

The document outlines control flow statements in Python, which include sequential, decision, and loop statements. It details various decision structures such as if, if...else, and nested if statements, as well as loop types like while and for loops. Additionally, it discusses jump statements like break, continue, and return, explaining their functions within control flow.

Uploaded by

bichumbk08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

CONTROL FLOW STATEMENTS

The control flow statements in Python Programming Language are:

1. Sequential Control Flow Statements


2. Decision Control Flow Statements
3. Loop Control Flow Statements
Sequential Control Flow Statements :
This refers to the line by line execution, in which the statements are executed
sequentially, in the same order in which they appear in the program.
Decision Control Flow Statements :
Depending on whether a condition is True or False, the decision structure may
skip the execution of an entire block of statements or even execute one block of
statements instead of other (if, if…else and if…elif…else)
if Decision Control Flow Statement
The syntax for if statement is:

if Boolean_Expression :

statement (s)

The Boolean expression after the if statement is called the condition.


The if…else Decision Control Flow Statement
The syntax for if…else statement is:

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 Loop For Loop Nested Loop


While Loop
The while loop is used to execute a set of statements as long as a condition is
false.
Syntax of while loop:

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

for iteration_variable in sequence:


statement(s)
range function:
range([start],stop,[step])
Example
To find the sum of all odd and even numbers upto a number specified by user.
Nested Loop
If a loop exists inside another loop it is called Nested loop
Syntax of Nested 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

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
Return
A return statement is also one type of jump statement. “return” statement
terminates a function and returns a value to the calling function.

You might also like