Control and Loop Statements in Python

Control Statements

There are many instances when it is required to take decision. If statement is a decision making statement. If statement in Python is used to test a condition and transfer the control of program to usual sequential next line of code or skips certain lines and jump to another point.

It should be clearly noted that Python make use of indentation almost every where especially while writing if statement.

If indentation is not used in a statement that particular line of code will be treated as not part of if statement or loop statements and is thus treated as statement outside the control and loop.


>>> x=10

>>> if x>0:

. . .      print(x,” is +ve Number”)    #indented line thus executed if x>0 TRUE

>>>print(x,” is -ve”)   # this will also be printed as unintended lines outside if

So, one should be very careful while writing Python statement, as if you forget to indent you line of code properly, the line may be treated as outside the if statement and give wrong answer.

Figure below shows the flowchart of the if-else statement. When the expression turns out to be TRUE the immediate next line of code in sequence are executed. If the expression is FALSE then the next line(s) of code are skipped.

Example below shows some of the correct ways of writing the if and else statement in Python


Flowchart of if else Statement

Flowchart If – elif Statement in Python





Python Syntax Python Example

If condition :

Statement(s)    #see indentation



if condition :






If condition :


elif condition :





There can be zero or more elif parts, and the else part is optional. The keyword ‘elif’ is short for ‘else if’, and is useful to avoid excessive indentation. An if … elif … elif … sequence is a substitute for the switch or case statements found in other languages.

Nested if Statement 

Python permits writing if statement inside another if statement. This way of writing a if...elif...else statement inside another if...elif...else statement is called nesting



>>> if x > 0 :

>>> , , , if x == 0:

>>> . . . . . . print (“x is zero”)

>>>  . . . else :

>>> . . .  . . . print(“Number is +ve”)

>>> else :

>>> . . . print (“Number is Negative”)



  1. Write a program in Python to print whether a number is +ve or -ve
  2. Write a program in Python to calculate the % of marks of a student.
Updated: April 19, 2019 — 11:01 am

Leave a Reply

Your email address will not be published. Required fields are marked *

care4you © 2020 Connect On Facebook Protection Status