0% found this document useful (0 votes)
55 views18 pages

PP Module-2

This document discusses control structures in Python including selection statements like if, if-else, and if-elif-else statements and iterative statements like while and for loops. It provides examples of the syntax for each statement and explains how to use break, continue, and pass statements to control loop execution.
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)
55 views18 pages

PP Module-2

This document discusses control structures in Python including selection statements like if, if-else, and if-elif-else statements and iterative statements like while and for loops. It provides examples of the syntax for each statement and explains how to use break, continue, and pass statements to control loop execution.
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/ 18

MODULE-2

CONTROL STRUCTURES

Yogendra Prasad P
Selection Statements
• In Python, the selection statements are also known as decision making
statements or branching statements.
• The selection statements are used to select a part of the program to
be executed based on a condition.
• Python provides the following selection statements.
1. if statement
2. if-else statement
3. if-elif-else statement
4. Nested-if statement

Yogendra Prasad P
if statement
• In Python, we use the if statement to test a condition and decide the
execution of a block of statements based on that condition result.
• The if statement checks, the given condition then decides the
execution of a block of statements.
• If it is True, then the block of statements is executed and if it is
False, then the block of statements is ignored.

Yogendra Prasad P
Cont.
• Syntax:
if condition_1:
Statement_1
…..

Yogendra Prasad P
if-else statement
• In Python, we use the if-else statement to test a condition and pick the
execution of a block of statements out of two blocks based on that
condition result.
• The if-else statement checks the given condition then decides which
block of statements to be executed based on the condition result.
• If the condition is True, then the true block of statements is
executed and if it is False, then the false block of statements is
executed.

Yogendra Prasad P
Cont.
• Syntax:
if condition_1:
Statement_1
…..
else:
Statement_2
…..

Yogendra Prasad P
if-elif-else statement
• In Python, When we want to test multiple conditions we use elif
statement.
• Syntax:
if condition_1:
Statement_1
…..
elif condition_2:
Statement_2
…..
else:
Statement_3
…..

Yogendra Prasad P
nested if statement
• Nested if statements are an if statement inside another if statement.
• Syntax:
if condition_1:
if condition_1a:
Statement_1
…..
else condition_2:
Statement_2
…..
else:
Statement_3
…..

Yogendra Prasad P
Iterative statement
• In Python, the iterative statements are also known as looping
statements or repetitive statements.
• The iterative statements are used to execute a part of the program
repeatedly as long as a given condition is True.
• Python provides the following iterative statements.
1. while statement
2. for statement

Yogendra Prasad P
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.
• This continues until the expression becomes false.

Yogendra Prasad P
Cont.
Syntax:
while condition:
Statement_1
Statement_2
Statement_3
...

Yogendra Prasad P
for loop
• A for loop is used to iterate over a sequence that is either a list, tuple,
dictionary, or a set.
• We can execute a set of statements once for each item in a list, tuple,
or dictionary.

Yogendra Prasad P
Cont.
Syntax:
for <variable> in <sequence>:
Statement_1
Statement_2
Statement_3
...

Yogendra Prasad P
Loop Control Statements
• Loop control statements change execution from its normal sequence.
• Python supports the following control statements.
1. Continue Statement
2. Break Statement
3. Pass Statement

Yogendra Prasad P
Cont.
Break Statement:
• The break statement is used to terminate the loop containing it, the
control of the program will come out of that loop.
Continue Statement:
• When the program encounters a continue statement, it will skip the
statements which are present after the continue statement inside the
loop and proceed with the next iterations.

Yogendra Prasad P
Cont.
Pass Statement:
• We use pass statement to write empty loops.
• Pass is also used for empty control statement, function and classes.
• Pass statement is python is a null operation

Yogendra Prasad P
while loop with ‘else’
• In Python, the else clause can be used with a while statement.
• The else block is gets executed whenever the condition of the while
statement is evaluated to false.
• But, if the while loop is terminated with break statement then else
doesn't execute.

Yogendra Prasad P
for loop with ‘else’
• In Python, the else clause can be used with a for a statement.
• The else block is gets executed whenever the for statement is does not
terminated with a break statement.
• But, if the for loop is terminated with break statement then else block
doesn't execute.

Yogendra Prasad P

You might also like