top of page

If-Else Conditions

Flow of control is implemented with three basic types of control structures:

  • Sequential: default mode. Sequential execution of code statements (one line after another)

  • Selection: used for decisions, branching -- choosing between 2 or more alternative paths. In Python, these are the types of selection statements:

    • if

    • if..else

    • if ..elif

    • Nested if

  • Iteration: used for looping, i.e. repeating a piece of code multiple times while condition evaluates to true. In Python, there are two types of loops:

    • for

    • while

Selection/ Decision making statements 

These are decision-making statements decide which statement to execute and when. Decision-making statements evaluate the Boolean expression and control the program flow depending upon the result of the condition provided.

Example:

num=8

if num>5:

     print("Value is above 5")

else:

     print("Value is less than 5")

Output:

Value if above 5

Concept of indent

Indentation refers to the spaces given at the beginning of the code. It is a way of telling a Python interpreter that the group of statements belongs to a particular block of code. 

python indentation.JPG

If..else Statement

Code

Output

c.PNG

If..elif Statement 

Code

if else.JPG

Output

Nested IF Statement 

Code

Output

nested if.JPG
bottom of page