[go: up one dir, main page]

0% found this document useful (0 votes)
26 views14 pages

Conditional and Looping Statements

The document discusses various Python control flow statements including if, if-else, if-elif statements for conditional execution and for, while loops for repetition. It also covers range(), break, continue and nested loops.

Uploaded by

Shubham
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)
26 views14 pages

Conditional and Looping Statements

The document discusses various Python control flow statements including if, if-else, if-elif statements for conditional execution and for, while loops for repetition. It also covers range(), break, continue and nested loops.

Uploaded by

Shubham
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/ 14

if Statement

• In Python, if statement is used to select statement


for processing. If execution of a statement is to be
done on the basis of a condition, if statement is to
be used. Its syntax is-
if <condition>:
statement(s)
like -

Neha Tyagi, KV 5 Jaipur


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 -

Neha Tyagi, KV 5 Jaipur


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

Neha Tyagi, KV 5 Jaipur


Nested If -else

Neha Tyagi, KV 5 Jaipur


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

Neha Tyagi, KV 5 Jaipur


range () Function
• In Python, 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].

Neha Tyagi, KV 5 Jaipur


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.

Neha Tyagi, KV 5 Jaipur


Table of a number by For loop

Syntax of For Loop

for <var> in <sequence>:


<statements to repeat> Output

Neha Tyagi, KV 5 Jaipur


Table of a number by while loop

Syntax of While Loop

While <LogicalExpression>:
<loop body with increment
or decrement>
Output

Start
Stop

Step

Neha Tyagi, KV 5 Jaipur


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

Neha Tyagi, KV 5 Jaipur


Jump Statements
break Statement

Output Output

Neha Tyagi, KV 5 Jaipur


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

Neha Tyagi, KV 5 Jaipur


Jump Statements
continue Statement

Output of both the


programs

Neha Tyagi, KV 5 Jaipur


Nested Loop

OUTPUT

Neha Tyagi, KV 5 Jaipur

You might also like