0% found this document useful (0 votes)
14 views15 pages

Python Branching

Uploaded by

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

Python Branching

Uploaded by

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

Python

Branching
Branching Using Conditional Statements and Loops

• Branching- Process of using conditions to execute certain set of


instructions in python program
• Condition satisfied – Program will execute one set of instructions
• Condition not satisfied- Program will execute other set of instructions
Branching with if,else and elif
• Part of conditional statements
• The if statement
• The if-else statement
• The nested-if statement
• The if-elif-else ladder
Python if statement
Syntax:
if condition:
statement1
statement2
Condition- value, variable or expression
If condition is true statements within if block are executed

Example:
#Example for if statement
x=12
y=20
if (x<y):
print(“x is less than y”)
If-else statement
• else statement with the if statement executes a block of code when the if
condition is false.
Syntax:
if (condition):
# Executes this block if condition is true
else:
# Executes this block if condition is false

Example:
x = 22
y = 20
if y > x:
print(“y is greater than x")
else:
print(“y is not greater than x")
Nested if statement
• If statements inside if statements-used to check for another condition after
a particular condition holds true
• Syntax
if (condition1):
# Executes when condition1 is true
if (condition2):
# Executes when condition2 is true
# if Block is end here
# if Block is end here

Example:
x = 41

if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")
Elif(else if) statement
• To run in a series of conditional blocks.
Syntax:
if (condition):
statement
elif (condition):
statement
elif(condition):
statement
Example:
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a != b:
print("a and b are unequal")
elif a == b:
print("a and b are equal")
If,elif,else
• Syntax
if (condition):
statement
elif (condition):
statement
.
.
else:
statement
Example:
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
for loop
• It is used for iterating over sequences(ex:lists,tuples,range,string)
• For loop executes set of statements for each item in the sequence
• Syntax:
for value in sequence:
statement(s)
Example:
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
for i in days:
print(day)
Iterating using range
• Used to create a sequence of numbers can be iterated in for loop
• Range(n) – creates sequence of numbers from 0-(n-1)
• Range(a,n)-creates sequence of numbers from a-(n-1)
• Range(a,n,step)-creates sequence of numbers from a-(n-1) with incremental step

Examples:
for i in range(5):
print(i)
Output:
0
1
2
3
4
While loop
• With the while loop we can execute a set of statements as long as a condition is true.
• Syntax:
while condition:
statement
Here statements executed until the condition becomes true.
Example:
i=1
while i < 6:
print(i)
i += 1 #remember to increment i, or else the loop will continue forever.(infinite loop)
Output:
1
2
3
4
5
Break Statement in Python
• The break statement is used to break out of a loop
• Used in for and while loops to alter the control flow
Example:
i=1
while i < 6:
print(i)
if i == 3:
break #exit the loop when i=3
i += 1
Output:
1
2
3
Break in for loop

Example:
for i in range(1, 10):
if i == 4:
break
print(i)
Output:
1
2
3
Continue Statement in Python

• used to stop the current iteration in a for loop or a while loop and continues with next iteration.
Example:
i=0
while i < 6:
i += 1
if i == 3:
continue
print(i)

Output:
1
2
4
5
6

You might also like