Conditional/Branching Statements in Python

Meidy Yolandia
2 min readDec 26, 2021

In programming, there are many control statements. Control statement is a type of instruction process flow to manage the execution of a command in python. An example of a control statement is a Conditional/Branching Statement.

Conditional/Branching Statements are control statements that are used to set when a command will be executed which is stated with a conditional statement. The condition must be True or False. If True, then the command will be executed. Then if False, then the command will not be executed.

Then what statement states that conditional statement? Let’s see the following explanation. Let’s go…

If Statement

In this statement, there is an If function, conditions that will be executed and there are also actions. For an example of its use:

Input:

roses = "red"
if roses == "red":
print("The color of roses is", roses)

Output:

The color of roses is red

This is an example of an if statement that evaluates to True. Then what if the command is False? Here’s an example:

Input:

x = 2 * 4
if x > 10:
print("The result is more than 10")

The program code above will not produce output, because the command is False so the program will not be executed.

If Else Statement

In this statement there is an If function, there is a condition to be executed later, there is an action, and also an Else function. If the command in the first condition is True, then the condition will be executed. Then if the command in the first conditions is False, then the condition in the Else command will be executed. For more details see the following example:

Input:

x = 5 + 7
if x > 20:
print("The result is more than 20")
else:
print("The result is less than 20")

Output:

The result is less than 20

If Elif Else Statement

In this statement, consisting of the If function, Elif function, Else function, a condition that will be executed, and there is also an action.

If the command in the first condition is True then the other conditions will not be executed, only the first action will be executed. If the command in the first condition is False, then the second condition in the Elif function will be checked. If the second condition is True then the second action will be executed. Then if the second condition is False, then the third condition will be checked and so on. If there is nothing in the program that is True, then the action to be executed is actionN. Here’s an example:

Input:

x = 5 ** 3
if x == 100:
print("The result is equal to 100")
elif x < 100:
print("The result is less than 100")
else:
print("The result is more than 100")

Output:

The result is more than 100

In this case, it produces the output “The result is more than 100” because the previous two conditions are False, then the program to be run is in the third condition.

--

--

Meidy Yolandia
0 Followers

Meidy Yolandia, K3521041, meidyyolandia@student.uns.ac.id , Sebelas Maret University, Bachelor Informatics and Computer Engineering Education Department