In programming, you don't run every command you write. Sometimes a few lines of a program have to be skipped so the code matches the logic you have in mind. You do this with a conditional statement, or what people usually call branching.
A conditional statement runs commands according to the condition in front of it. In Python, the conditional statement is handled by the if statement.
This time you'll see how to use branching in Python, starting from the simplest shape and building up from there.
Let's start.
Using the if Statement in Python
The if statement in Python makes a decision based on a boolean condition. When the boolean condition is true, the code block in question runs. If the condition is false, the program skips that block and moves straight on.
if <kondisi>:
<statement>
<kondisi>: A boolean expression that is valid in Python. It can be a comparison or a logic operation.<statement>: Python code that must be indented—why?. This code runs when the boolean expression is true.
Example:
x = int(input("masukkan nilai x: "))
y = int(input("masukkan nilai y: "))
if x < y:
print("x kurang dari y")
if x > y:
print("x lebih dari y")
print("x={}, y={}".format(x, y)) # always runs
Output with the input 5 and 10:
masukkan nilai x: 5
masukkan nilai y: 10
x kurang dari y
x=5, y=10
In that example, x is less than y, so the statement that runs is the one in the first if condition.
Using the if...else Statement in Python
The else statement is for the case you already know about. You know there is a possible condition that the if statement will not satisfy, so you prepare a second block for it.
if <kondisi>:
<statement>
else:
<statement>
When the condition is not met, the program jumps straight to the block of commands inside else.
Example:
x = int(input("masukkan nilai x: "))
y = int(input("masukkan nilai y: "))
if x < y:
print("x kurang dari y") # runs when the condition is met
else:
print("x lebih dari y") # runs when the condition is not met
print("x={}, y={}".format(x, y)) # always runs
Output with the input 9 and 6:
masukkan nilai x: 9
masukkan nilai y: 6
x lebih dari y
x=9, y=6
Because x is greater than y, the condition in the if statement is not met. The program then runs the command inside the else statement.
But wait. What happens if you feed it two numbers that are the same? Take 5 as an example.
Output with the input 5 and 5:
masukkan nilai x: 5
masukkan nilai y: 5
x lebih dari y
x=5, y=5
Something looks wrong there. It should say x is equal to y, because both values are 5.
That's fair enough. You never told the computer what to output when the variables x and y hold the same value.
Now your job is to tell the computer about one more condition that isn't there yet. For that, you can use the elif statement.
Using the if...elif...else Statement
The elif statement works roughly like an else joined to an if. Just like else, when the condition in the if statement is not met, the program jumps to the elif statement.
What sets it apart from else is the check. The elif statement tests a condition first before it continues running the program.
if <kondisi>:
<statement>
elif <kondisi>:
<statement>
else:
<statement>
Example:
x = int(input("masukkan nilai x: "))
y = int(input("masukkan nilai y: "))
if x < y:
print("x kurang dari y") # runs when the condition is met
elif x > y:
print("x lebih dari y") # runs when the if condition is not met
else:
print("x sama dengan y") # runs when no condition is met
print("x={}, y={}".format(x, y)) # always runs
Output with the input 5 and 5:
masukkan nilai x: 5
masukkan nilai y: 5
x sama dengan y
x=5, y=5
Yay. The output comes out the way it should.
Closing
By this point you should have the concept in hand. That's all for this one, and I hope it's useful. Next time you'll look at repetition with the FOR loop in Python 3.
See ya...