Conditional statements are often used to control the flow of the program as we desire.
It’s represented in the following syntax:
The statement block will only be executed if the expression is evaluated to be true.
Example:
The program above will only increment a by one if frst and scnd have the same value.
It’s represented in the following syntax:
The statement1 block will only be executed if the expression is evaluated to true, if not then it will execute the statement2.
Example:
The program above would increment frst by one if frst and scnd had the same value, but since they don’t it will instead execute the else block and decrement a by 1.
Elif is short for else if and unlike else that is tested if all other conditions fail, elif will be tested as an else but gives us the chance to specify what is the condition we want.
Here is an example:
In this case, first condition not satisfied it moves to second condition elif and it meets
Python also has the capability of nesting conditional statements inside one another. Following the syntax:
This can go on and on as you desire the code to work.
Example:
In the portion of the code above, the first if statement will evaluate to true because frst indeed equals scnd and therefore it will execute that block. Inside that, if there is another one that evaluates if scnd is equal to thrd + 1 it evaluates to true to executing a -= 1.