 Decision Making in Python | if statement in python  # Decision Making in Python

2476

Decision-making statements are used when conditional choices are required for execution of code block or in those cases, where logical decisions are required i.e. where multiple expressions can be easily evaluated with a result of true or false. Decision-making statements are helpful in managing the flow of program execution.

Decision-making statements found in python programming language are given below:
* If statement
* If-else statement
* Nested if statement

if statement
This decision-making statement is used to control the execution of the block of statements i.e. the block of statements will execute only when a certain condition is true otherwise not.

Syntax
if expression:
#code execution

Program

`p = 87if p > 50:   print("p is greater")`

Output
p is greater

if else statement
When we want to do something else if the condition is wrong then if else statement is used. Else statement is used along with if statement for executing a block of code when the condition is not true.

Syntax
If (condition):
# this block will execute if the condition is true
Else:
# this block will execute only if the condition is false

A simple python program illustrating if else statement

`g = 75h = 98if g > h :    print ("g is greater")else :    print ("h is greater")`

Output
h is greater

nested if statement
Python language permits nesting of if statement within another if statements i.e. inside one statement there is another if statement.

Syntax
if ( first condition):
# Statement execute when the first condition is true
if (second condition):
#statement execute when the second condition is true
# if block ends

A program illustrating nesting if statement usage in python language

`s = 20if (s == 20):     # First if statement     if (s < 25):         print ("s is smaller than 25")     # Nested - if statement     # executed only ablove if statement is true    if (s < 22):         print ("s is smaller than 22 too")     else:         print ("s is greater than 25")`

Output
s is smaller than 25
s is smaller than 22 too

Another program illustrating nested if statement

`number = -76if number > 0:    print(" The number is a Positive number")elif number == 0:    print(" The number is Zero")else:    print("The number is negative number")`

Output
The number is negative number

Elif Statement
The elif statement is optional that is identical to the else. By following an if, there can be an arbitrary number of elif statements. If none of the above statement conditions are true, then the final else statement will be executed.

Syntax
if expression:
# code execution
elif expression:
# code execution
else:
# code execution

A simple program illustrating elif statement

`r = 70q = 75if r > q:   print("r is greater")elif r == q:   print("both numbers r and q are equal")else:   print("q is greater")`

Output
q is greater Share:  ## Get a .COM for just \$6.98

Secure Domain for a Mini Price    