Intro to Programming Tutorial menu

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:

OperatorMeaningExampleResult
==is equal to5 == 5true
!=is not equal to5 != 3true
>is greater than10 > 20false
<is less than10 < 20true
>=is greater than or equal to18 >= 18true
<=is less than or equal to7 <= 6false

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

python
marks = 72
if marks >= 50:
    print("You passed!")
print("Result checked.")
Output
You passed!
Result checked.

Example

javascript
let marks = 72;
if (marks >= 50) {
  console.log("You passed!");
}
console.log("Result checked.");
Output
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

python
balance = 400
price = 650
if balance >= price:
    print("Payment successful")
else:
    print("Not enough balance")
Output
Not enough balance

Example

javascript
let balance = 400;
let price = 650;
if (balance >= price) {
  console.log("Payment successful");
} else {
  console.log("Not enough balance");
}
Output
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

python
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")
Output
Grade A

Example

javascript
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");
}
Output
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: and in Python, && in JavaScript
  • At least one must be true: or in Python, || in JavaScript
  • The opposite: not in Python, ! in JavaScript

A cinema in Karachi gives a discount to students or to people aged 60 and above:

Example

python
age = 21
is_student = True
if is_student or age >= 60:
    print("Discount ticket: Rs 500")
else:
    print("Normal ticket: Rs 900")
Output
Discount ticket: Rs 500

Example

javascript
let age = 21;
let isStudent = true;
if (isStudent || age >= 60) {
  console.log("Discount ticket: Rs 500");
} else {
  console.log("Normal ticket: Rs 900");
}
Output
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")

Learn Intro to Programming with a teacher

Join the Python Programming course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp