top of page
Search
Writer's pictureMayuri Kale

Decision making in Python - if-else statement




The decision-making statement is intended to regulate the flow of programme execution based on the condition. If the condition is true, the block will proceed; if the condition is false, the block will not accomplish.


Decision-making Statement



Selection statements are also considered to as decision making statements or branching statements in Python. Selection statements are employed to decide a section of the programme to run based on a criteria. The following selection statements are available in Python.



We often see situations in programming where we must pick which block of code should be executed based on a condition.




if statement

A boolean expression contains one or more statements in an if statement.



if-else statement


When a boolean expression is FALSE, an if statement might be proceeded by an optional else statement.

If a condition is true, the if statement will operate a block of statements; if the condition is false, the if statement will not execute a set of statements. What if the condition is false and we want to conduct something different? The else clause is now in play. When the condition is false, we may use the else statement in association with the if statement to run a block of code.

Syntax:

if (condition):


# Executes this block if

# condition is true

else:

# Executes this block if

# condition is false



Example

# python program to illustrate If else statement

#!/usr/bin/python

i = 40

if (i < 20):

print("i is smaller than 20")

print("i'm in if Block")

else:

print("i is greater than 20")

print("i'm in else Block")

print("i'm not in if and not in else Block")


Output:

i is greater than 20

i'm in else Block


i'm not in if and not in else Block


if-elif statement



Statements nested


One if or else if statement can be placed within another if or else if statement (s).



Conclusion


In this blog , we learned about python decision making , decision making statement in python if else. Visit - to learn another decision making statements in python .




9 views0 comments

Recent Posts

See All

Comments


Post: Blog2_Post
bottom of page