Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

CONTROL STATEMENTS

Name: Amey Pradeep Salvi


Class: TYBCS Div: A
Roll No: 106
Subject : Python Activity
What are the Control Statements?
• In python, control statements decide the flow of execution
in a program.
• During the execution of a program, the Python interpreter
should go to each statement. Here, the control statements
decide where the Python interpreter should go. That is,
whether it should go to the next line or to any other part of
the code.
• There are three main control statements in Python. They
are:
• Conditional statements
• Loop statements
• Loop control statements
What are the Conditional Statements?

• In python, conditional statements handle the


decisions. So, these are also called decision-making
statements. Under these conditional statements, there
will be a block of code. The execution of that block
depends on the validity of the given condition.
• Mainly, there are three types of conditional statements
:
1. if statement
2. if-else statement
3. if-elif statement
1. If Statement
• The If statement evaluates the given condition, and decides to
execute the block of code or not.
• If the condition in the if statement is valid, it executes the block
of code.
• Otherwise, it skips the block of code and goes to the next block.
• For Example:
a=40
b=20
if (a>b):
print("a is greater than b")
Output:
a is greater than b
2. If else Statement
• The if-else statement evaluates the given condition in
the if statement. If the condition is True, it executes
the code in the if block. And, if the condition False, it
executes the code in the else block.
• For Example:
num1 = 407
if (num1%2 == 0):
print("407 is even number")
else :
print("407 is odd number")
• Output:
407 is odd number
3. If-elif Statement
• The if-elif statement is used to check multiple conditions.
• You can also write the condition in the elif statement.
• The condition in the elif statement is checked only if the condition
in the if statement is not True.
• For Example:
light = "yellow"
if (light== "green"):
print("you can move")
elif (light == "yellow"):
print("get ready to move")
elif (light == "red"):
print("stop")
• Output:
get ready to move
What are Loop Statements?
• In programming, there may be times when you
want to execute a block of statements repeatedly.
There we can use Loop statements.
• Looping means being able to repeat the
execution of code block more than once.
• In Python, there are two loop statements.
1. for loop 
2. while loop
1. For loop
• For loop is a looping statement in python that iterates over each item in
the sequence. It is used when we know the number of times the block is
going to execute.
• The for loop runs a block of code until the loop has iterated over all
elements in a sequence.
• It is mainly used to iterate the elements in the sequential data types. 
• For Example:
list1 = [89,67,56]
for i in list1:
x = i+10
print(x)
• Output:
99
77
66
2. While loop
• The while loop runs a block of code as long as the given condition is
True.
• When the condition returns False, the control goes out of the loop
and executes the next statements. 
• For example:
count = 4
while count>0:
x = count*10 print(x)
count = count-1
• Output:
40
30
20
10
What are Loop Control Statements?
• Loop control statements are used in for loop and
while loop.
• The loops will iterate as long as the condition is True.
• But, in some cases, you might want to come off the
loop, or you want to skip the execution of a block of
code, for a particular condition.
• In such cases, loop control statements comes handy.
• There are three loop control statements in Python:
1. break statement
2. continue statement
3. pass statement
1. Break Statement
• The break statement terminates the execution of the loop.
• We generally use the break inside the conditional statement in for or
while loop.
• When the Python interpreter encounters the break statement, it exits
the loop immediately. And, executes the further statements.
• For Example:
for i in "203546789":
if(i=="4"):
break
print(i)
• Output:
2
0
3
5
2. Continue Statement
• Continue statement skips the rest of the code from being executed,
for the current iteration.
• It indicates that if the continue statement is executed, the
statements in the loop after it will not run for the present iteration.
• For Example:
for i in range(5):
if(i== 2):
continue
print(i)
• Output:
0
1
3
4
3. Pass Statement
• Loops and conditional statements cannot include any empty code
because the python interpreter raises an error.
• So, you can use the pass statement in these situations.
• Pass statement serves as a placeholder for code that we want to add later.
• You can use this statement when there’s a syntactic requirement but not
an operational requirement.
• For Example:
str1 = "10120120210210"
for var in str1:
if var == "0":
pass
else:
print(var, end = "")
• Output:
112122121
Write a python program to check largest
among the three?
Program:
a=int(input("Enter First No"))
b=int(input("Enter Second No"))
c=int(input("Enter third No"))
if(a>b and a>c):
print(a,"is Greater")
elif(b>a and b>c):
print(b,"is Greater")
else:
print(c,"is Greater")

• Output:
Enter First No5
Enter Second No6
Enter third No7
7 is Greater
THANK YOU

You might also like