Conditional Statements

Decision Making Statements

Decision making statements in programming languages decides the direction of flow of program execution. Decision making statements available in python are:

  1. if statement

  2. if..else statements

  3. if-elif ladder

If statement

if statement is the most simple decision making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.

Example 1:

#syntax
#if condition:
#statement1
#statement2
#program to illustrate If statement  
i = 10
if (i > 15): 
    print("10 is less than 15") 
print("I am Not in if") 

I am not in if

if- else

  1. The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t.

  2. Here comes the else statement. We can use the else statement with if statement to execute a block of code when the condition is false.

syntax

if (condition):

else:

#Executes this block # if condition is false

Example 2:

i is greater than 15

i'm in else Block

Example 3:

Enter a number : 689

Odd

Example 4:

Enter x : 45

Enter y : 67

Enter z : 12

67 is greater than 45 and 12

if-elif-else

Here, a user can decide among multiple options. The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

Syntax

Example 6:

x : 45

y : 67

z : 12

67 is greater than 45 and 12

Example 7:

A : 45

B : 68

C : 93

C is Greatest

Example 8:

X : 45

Y : 67

Choose one of these :

1.Addition

2.Subtraction

3.Multiplication

4.Division

5.Modules

6.Floor Division

7.Exponent(Power)

Your Choice : 1

Addition of 45 and 67 is 112.

Example 9:

Output:

Last updated