Skip to main content

Control Flow Statement in Python

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 1Example 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


ProgramFlow 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

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

Popular posts from this blog

Q & A Chapter 1 | Internet Basics | Class 10 CBSE | Computer Application

 Q & A of Chapter 1 Internet Basics A. Tick the correct options – 1. (b) TCP/IP 2. (a) Internet Service Provider 3. (a) URL 4. (b) HTTP 5. (a) Bridge 6. (a) home page 7. (d) All of these 8. (b) Domain Naming System 9. (b) Rules 10. (d) ARPANET B. Very Short Answer Type Questions – 1. In the URL http://www.myfirm.co.in/about.html , what do the following components depict? (a) http                                                 (b) in (a) Http represents a type of server and the protocol on the Internet (b) ‘in’ represents the domain extension of the website. 2. In the URL http://www.favmusic.com/contactus.html what does www.favmusic stand for? www.favmusic represents the domain name i.e. favmusic in www. 3. W...

Bit by Bit | Chapter 6 - Graphics and Sound in QB64 | QBasic | Class 7 CBSE | Computer Application

Bit by Bit | Chapter 6 - Graphics and Sound in QB64 A. Tick the correct option. 1. (d) screen mode 0 2. (b) 640 x 350 3. (d) White 4. (a) Lower-Right 5. (c) PSET 6. (c) Generate a tone for a short duration B. Fill in the blanks. 1. CIRCLE 2. PAINT 3. PLAY 4. Graphic 5. Pixels 6. Box C. True or False. 1. True 2. False 3. False 4. True 5. True 6. True D. Answer the following questions. 1. What is the use of SCREEN statement? The SCREEN statement is used to set the screen mode for displaying graphics. There are many screen modes and each screen modes have their own number which is from SCREEN 1 to SCREEN 13. The various screen modes differ in resolution. 2. What is a pixel? How is it related to resolution? The graphics which is displaying on the computer screen is made up of thousands of dots called pixel. Pixel stands for picture element. The number of pixels in an image determines the resolution of the image. Higher the resolution, cleaner the image will appear. 3. What is the differenc...

Bit by Bit | Chapter 3 Working with Queries, Forms and Reports | Class 8 CBSE | Computer Application

Working with Queries, Forms and Reports Exercise Solution A. Tick the correct option – 1. (d) Datasheet View 2. (c) Three 3. (c) Four 4. (b) Datasheet View 5. (d) Filter 6. (b) Create B. Fill in the blanks – 1. Query 2. Form 3. Objects 4. Design 5. Report 6. Result C. State True or False – 1. T 2. F 3. T 4. T 5. F 6. T D. Answer the following questions – 1. What is Query? A query is a method of getting answers to questions on data. It is used to retrieve selective data satisfying specific criteria. 2. Differentiate between Form and Report. Forms allow you to add and update data in a table, one record at a time, while reports are an effective way to present data in printed format. You can customize the appearance of tabular data on a report. 3. How can you specify multiple conditions while designing a query? Specifying the condition in the Criteria row for more than one field will result in a query that will select only those records...