Krivalar Tutorials
Krivalar Tutorials

# Python - if elif else statement Syntax Example

### <if...else Statement    Nested if Statement >

If you want to check more than two possibilities, you need to have more than two branches. This is done by using a chained conditional statement or if...elif..else statement.

## if...elif...else Control Statement

• The elif is an abbreviation of else if.
• There is no limit to using elif.
• You can use the number of elif for checking more than two conditions.
• Each condition is checked in order.
• If one is False, the next will be checked, and so on.
• If one of them is True, that corresponding branch will be executed and then the conditional statement will end.
• Suppose, if more than one condition is true, in that case only the first True branch will be executed.

## Syntax of Python if...elif...else

Following is the basic syntax of the if..elif..else decission control flow statement.

```if expression_1:
Statements
elif expression_2:
Statements
elif expression_3:
Statements
else:
Statements
```

## Example with if...elif...else in Python

Below is an example proram to explain the if...elif...else branching in python .

```A = int(input('enter the first value: '))
B = int(input('enter the second value: '))

if(A>B):
print('The value A greater than B')
elif(A<B):
print('The value A is less than B')
elif(A==0)and(B==0):
print('Both A and B are equal to Zero')
else:
print('Both A and B are equal')
```

While executing above program you will get the following output.

Test_Case 1:

```F:\>python conditionstm.py
enter the first value: 20
enter the second value: 3
The value A is greater than B
```

Test_Case 2:

```F:\>python conditionstm.py
enter the first value: 20
enter the second value: 30
The value A is less than B
```

Test_Case 3:

```F:\>python conditionstm.py
enter the first value: 0
enter the second value: 0
Both A and B are equal to Zero
```

Test_Case 4:

```F:\>python conditionstm.py
enter the first value: 4
enter the second value: 4
Both A and B are equal
```