The Condition Statements is very important for each and every one. Let me tell you, in real time we need to take some decision based on some conditions. In simple words “if the rain will come then I will stay at home otherwise I will go out.” Here the rain is the condition and you take the decision based on rain is come or not.

Same situations in the programming language, we have some conditions and we have to make some decisions and based on that the program will be execute.

So, In the programming language most of the time we need to control the flow of execution of the code. Or we can say the program is execute till the condition is not satisfy and in any case the condition is not satisfy then that time program will display some another statements.


The Condition Statements also known as Decision-making Statements, Flow-control Statements. And we can say this statement is used when we want to execute some block of statements when the given condition is TRUE or FALSE.


Various Condition Statements:

  • If statements
  • If-else statements
  • elif statements
  • Nested if and if-else statements

1. If Statements:

This condition statement is most commonly used in programming language. Simple if condition is checking the condition if the condition is TRUE then and only it will execute the block of statements inside the If statement.

Syntax:

If (Boolean condition):

               #block of statements

Example:

If Condition

2. If-else statements:

This condition statement is used when your given condition is TRUE then it will execute the IF block and if the condition is FALSE then it will execute the ELSE part.

So, the else part is execute only when the condition become false.

Syntax:

If (condition):

               #block of statements

else:

               #block of statements

Example:

IF_else

 3. Elif statements:

The elif statement is used when you want to check multiple conditions only if the given IF condition is FALSE. It is similar to the if else condition but the difference is only here you can check multiple conditions when your IF condition is FALSE.Else is execute only when the IF and ELIF both condition statements are FALSE.

Syntax:

If (condition):

            #block of statements

elif (condition):

           #block of statements

else:

           #block of statements

Example:

if_elif

4. Nested if and if-else statements:

Nested if-else statements it means the if condition or if-else condition is present inside the another if or if else condition. Simple meaning if condition is in the another if condition. It is used for checking the multiple condition in the program.

Syntax:

If (condition):                                                                       If (condition):

if (condition):              

                    if (condition):                                            

                                 #block of statement              

                    else:                                                                                            #block of statement                   else:                                                                  

                     #block of statement

Example:

nested_if