Python If-else statements – Python decision making

Python If-else statements – Python decision making

Decision making is the most important aspect of almost all the programming languages. As what we understand from the name, decision making allows us to run a particular block of code for a particular decision. Here, the decisions are made on the validity of the particular conditions. Condition checking is the backbone of decision making.

We have the following statements in python, to perform decision making.

Statement

Description

   

If Statement

The if statement is used to test a specific condition. If the condition available in if statement is true, a block of code in the (if-block) will be executed.

Example :

if(condition){

    #Code to be executed

}

If - else Statement

The if-else statement is similar to if statement except the fact that, it also provides the block of the code for the false case of the condition to be checked. If the condition provided in the if statement is false, then the else statement will be executed.

Example :

if(condition){

    #Code to be executed

}else{

    #Code to be executed

}

Nested if Statement

Nested if statements enable us to use if? else statement inside an outer if statement.

if(condition){

    #Code to be executed

}else if(condition){

    #Code to be executed

}

Enjoyed this article? Stay informed by joining our newsletter!

Comments

You must be logged in to post a comment.

Related Articles
About Author