In Python there are also multiple branches represented by the elif
statement
The if
-conditions are evaluated in sequence until one of them is true
. Then finally the code section belonging to this condition is executed and the multiple branching handling is finished. The else
-part is only executed if none of the conditions is true
.
Line | Description |
---|---|
1 | Prompts the user to enter a number. This is stored in the variable firstNumber |
2 | Prompts the user to enter a number. This is stored in the variable secondNumber |
4 – 5 | If the value of the variable firstNumber is greater than the value of the variable secondNumber , the string “value_first_variable > value_second_variable” is output |
6 – 7 | If firstNumber is smaller than secondNumber , the string “value_first_variable < value_second_variable” is output |
8 – 9 | Otherwise the string “value_first_variable = value_second_variable” is output |
firstNumber = input("Please enter first number: ")
secondNumber = input("Please enter second number: ")
if (firstNumber > secondNumber):
print(firstNumber + " > " + secondNumber)
elif (firstNumber < secondNumber):
print(firstNumber + " < " + secondNumber)
else:
print(firstNumber + " = " + secondNumber)
Please enter first number: 5
Please enter second number: 8
5 < 8