Control Flow Statement in Python
A computer always executes a program sequentially. It starts
execution from the first statement and ends at the last statement. This order
of execution of statement(s) is called Flow of Control. But if you want some
statements to print after fulfilling some condition then this can be achieved
by using the control flow statement.
The control flow statement or control statements are used
to control the flow of execution of the program. There are three different
types of control flow statements in python:
- Sequential
- Decision making or selection
- Looping or iterative
Sequential – Sequential execution of statements is the
default behaviour of the program, in which it executes one line after another.
Decision Making or Selection – Decision making statement is
also called selection statement. Here, the execution of the statements depend
upon the condition. Once the condition is fulfilled then the control of the
flow will move accordingly. In python there are three types of selection
statements:
- If
- If-else
- If-elif-else
Looping or iterative – used for repeating a set of
statements multiple times in a row. In python there are two types of loops:
- While
- For
Simple program and its Flow chart
Example 1 | Example 2 |
print(“Hello World”)
print(“I am”)
print(“Python”)
print(“Programming”) |
a=10
b=20
c=a + b
print(“Addition=“,c) |
|
Simple Program Using Control Flow Statement
Program | Flow Chart |
a = input(“Enter any text:: ”) if a = “bad morning”: print(“Sorry, I cannot print this text”) else: print(a) |
IF ELSE STATEMENT
A ‘if’ statement is used to execute the specific block of
code based on a condition.
If the condition is
‘true’ then the True statement block will execute and if the statement is
‘false’ then the False statement block will execute.
Flow chart to check odd or even number
Flow chart to check odd or even number
Simple program to check odd or even number
num =
int(input(“Enter a number: “))
a = num%2
If a == 0:
print(“The number is Even”)
else:
print(“The number is Odd”)
Output –
Enter a
number: 5
The number
is Odd
Enter a
number: 4
The number
is Even
IF ELIF ELSE STATEMENT
A ‘if’
statement is used to execute the specific block of code based on a condition,
if that ‘if’ condition does not give the True result then it moves on to ‘else
if’ or elif statement and it continues to do this until it reaches the else
statement.
Check the greatest number among
three number
print("Enter any three numbers: ")
a=int(input())
b=int(input())
c=int(input())
if a>b and a>c:
print("First no is
greatest")
elif b>c and b>a:
print("Second no is
greatest")
else:
print("Third no is
greatest")
Output –
Enter any
three numbers:
34
45
33
Second no is greatest
Comments
Post a Comment
Students you can ask your doubts in comment section