Control Flow
Control Flow:-Control Flow statements used to change the normal flow of execution of the program.
Three type of control flow statements in python
if condition : statements
name=input("Enter Name:")
if name="rahul":
print("Hello Rahul Good Morning")
else:
print("Hello Guest Good Moring")
print("How are you")
Output:
Enter Name:rahul
Three type of control flow statements in python
- Conditional statements
- Looping statements / Iterative statements
- Transfer statements
1. if:-
if condition is true then statements will be executed.
if condition : statements
OR
if condition:
statements-1
statements-2
statements-3
---------------
---------------
statements-n
Ex1:-
print("begin")
x=input("enter a +ve number")
i=int(x)
if i<10:
print("given number is 1 digit number")
print("end")
Output :
begin
enter a +ve number : 5
given number is 1 digit number
end
Ex2:-
name=input("Enter Name:")
if name=="rahul" :
print("Hello Rahul Good Morning")
print("How are you")
Output:
Enter Name:rahul
Hello Rahul Good Morning
How are you
2. if-else:-
if condition is true then Action 1 will be executed otherwise Action 2 will be executed.
if condition:
Action 1
else:
Action 2
Ex1:-
print("begin")
x=input("enter a +ve number")
i=int(x)
if i<10:
print("given number is 1 digit number")
else:
print("given number is >=2 digit number")
print("end")
Output:
begin
enter a +ve number25
given number is >=2 digit number
end
begin
enter a +ve number5
given number is 1 digit number
end
Ex2:-
if name="rahul":
print("Hello Rahul Good Morning")
else:
print("Hello Guest Good Moring")
print("How are you")
Output:
Enter Name:rahul
Hello Rahul Good Morning
How are you
Enter Name:ravi
Hello Guest Good Moring
How are you
3. if-elif-else:-
Based condition the corresponding action will be executed.
if condition:
Action 1
elif condition1:
Action 2
elif condition 2:
Action 3
-------------------
-------------------
else:
Default action
Ex1:-
print("begin")
x=input("enter a +ve number")
i=int(x)
if i<10:
print("given number is 1 digit number")
elif i<100:
print("given number is 2 digit number")
else:
print("given number is 3 digit number")
print("end")
Output:
begin
enter a +ve number500
given number is 3 digit number
end
Post a Comment
0 Comments