A program that always does exactly the same thing, no matter what, is a bit like a light with no
switch. The moment a program has to choose — turn left or right, pass
or fail, charge full price or give a discount — it needs a way to look at the
situation and pick. That power to choose is called selection, and in almost every
programming language it is spelled if … else.
The idea is simple enough to say in one line. The program tests a condition — a
question that comes back either true or false (a
Exactly one of the two blocks runs — never both, never neither. That is the whole heart of it; everything below is just this idea, explored.
Say a website lets you sign up only if you are age, and the condition is the question "is age at least 13?",
written age >= 13. Press Run and watch which branch fires — then
change the age and Run it again.
Read it out loud and it almost sounds like English: if age is greater-than-or-equal-to 13,
print the welcome; otherwise, print the refusal. The { … } curly braces mark
where each block starts and ends, and the condition in the round ( … ) brackets is the
thing being tested.
A condition is built from a comparison. You already met one, >=.
Here is the full set you will use every day — each one asks a yes/no question about two values and
hands back a boolean:
a === b — is a !== b — is a > b — is a < b — is a >= b — is a <= b — is A comparison isn't only for deciding — it produces a real true/false value you can print and look at. Run this to see the booleans that conditions are made of:
Whatever a condition works out to — true or false — is exactly what the
if uses to choose its branch.
Programmers often draw selection as a flowchart. The condition is a diamond with one arrow coming in and two arrows going out: a true path and a false path. Whichever way the answer points, that is the block that runs — and then both paths join back together and the program carries on. Here is our age check drawn out:
Every if/else you ever write has this exact shape underneath: one
question, two ways out, back to one road.
else if
Life isn't always two-way. A test grade might be A, B, C or Fail. You can chain conditions with
else if: the program checks each one in order and stops at the
first that is true, running that block and skipping the rest. A final plain else
catches everything that fell through. Run it and try different marks:
Order matters. Because the checks run top to bottom and stop at the first hit, a
mark of mark >= 80 straight away and never even
looks at the lower conditions. If you wrote the smallest test first, every passing mark
would grab it and nobody would ever get an A.
A classic use of selection is checking whether a number is even. The trick is the
remainder operator %, which gives what's left after dividing:
n % 2 is n % 2 === 0 asks
"does 2 divide it exactly?"
Notice how wrapping the decision in a function means we can ask the same question about many numbers
without rewriting the if each time — selection and functions team up beautifully.
The else is optional! Sometimes you want to do something only when a
condition holds, and otherwise just carry on quietly. Then you write an if on its own:
If the shopper is 16 or older, the block is simply skipped and price stays as it was.
No else needed.
&& and ||
Sometimes one test isn't enough. You can join booleans with and (&&
— true only when both sides are true) and or (|| — true when
at least one side is true). A theme-park ride might need you to be tall enough and
with an adult:
Swap && for || and the rule loosens to "tall enough OR with an
adult". Choosing the right connector is choosing exactly what your program means.
The single most common beginner bug in a condition is writing = when you mean
===. They look almost the same but do completely different jobs:
= is assignment — it puts a value into a variable.=== is comparison — it asks whether two values are equal,
giving back true or false.
So if (grade = 5) does not test whether grade is 5 — it
sets grade to 5 and then treats that as the condition, which quietly
breaks your logic (and TypeScript will usually refuse to compile it at all). Always use the
triple equals in a condition:
A handy memory hook: one equals gives a value, three equals checks a value.