Conditions: if and else
Conditions let a program make decisions: do one thing if something is true, and something else if it is not.
Asking Yes-or-No Questions
Every decision starts with a question that has a yes or no answer. In code, the answer is True or False (in JavaScript, true or false). We ask these questions with comparison operators:
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | is equal to | 5 == 5 | true |
!= | is not equal to | 5 != 3 | true |
> | is greater than | 10 > 20 | false |
< | is less than | 10 < 20 | true |
>= | is greater than or equal to | 18 >= 18 | true |
<= | is less than or equal to | 7 <= 6 | false |
Remember: one = puts a value into a variable. Two == compare two values. Mixing them up is one of the most common beginner mistakes. In JavaScript, use === for comparing; it is stricter and safer than ==.
The if Statement
Here Ali checks whether he has passed. The pass mark is 50.
Example
marks = 72
if marks >= 50:
print("You passed!")
print("Result checked.")You passed! Result checked.
Example
let marks = 72;
if (marks >= 50) {
console.log("You passed!");
}
console.log("Result checked.");You passed! Result checked.
Look at how each language marks the block of code that belongs to the if:
- Python uses a colon
:and then indentation (4 spaces at the start of the line) - JavaScript puts the condition in round brackets
( )and the block in curly braces{ }
The last line is not inside the block, so it always runs.
Adding else
Use else for what should happen when the condition is false.
Example
balance = 400
price = 650
if balance >= price:
print("Payment successful")
else:
print("Not enough balance")Not enough balance
Example
let balance = 400;
let price = 650;
if (balance >= price) {
console.log("Payment successful");
} else {
console.log("Not enough balance");
}Not enough balance
Only one of the two blocks runs, never both.
More Than Two Choices
When you have several options, check them one after another. Python uses elif, JavaScript uses else if.
Example
marks = 83
if marks >= 90:
print("Grade A+")
elif marks >= 80:
print("Grade A")
elif marks >= 70:
print("Grade B")
else:
print("Keep working hard")Grade A
Example
let marks = 83;
if (marks >= 90) {
console.log("Grade A+");
} else if (marks >= 80) {
console.log("Grade A");
} else if (marks >= 70) {
console.log("Grade B");
} else {
console.log("Keep working hard");
}Grade A
The computer checks from the top and stops at the first condition that is true. 83 is not 90 or more, but it is 80 or more, so it prints "Grade A" and skips the rest.
Combining Conditions
Sometimes you need two things to be true, or at least one of them. The words differ a little between languages:
- Both must be true:
andin Python,&&in JavaScript - At least one must be true:
orin Python,||in JavaScript - The opposite:
notin Python,!in JavaScript
A cinema in Karachi gives a discount to students or to people aged 60 and above:
Example
age = 21
is_student = True
if is_student or age >= 60:
print("Discount ticket: Rs 500")
else:
print("Normal ticket: Rs 900")Discount ticket: Rs 500
Example
let age = 21;
let isStudent = true;
if (isStudent || age >= 60) {
console.log("Discount ticket: Rs 500");
} else {
console.log("Normal ticket: Rs 900");
}Discount ticket: Rs 500
In Python, indentation is not just for looks: it decides which lines are inside the if. A wrong indent gives an IndentationError or a different result. Use 4 spaces every time.
Exercise
Exercise
In Python, what does this print? x = 5, then: if x > 10: print("big") else: print("small")
