seegongsik
Programming

How do if statements work

In the last lesson we met true and false. A program uses that true or false to choose a path: this way if the condition holds, that way if it doesn't. It's the first moment a program makes a decision on its own.

01

True goes this way, false goes that way

Sometimes a program comes to a fork in the road.
"If so, do this; otherwise, do that."
Which path it takes depends on whether the condition is true or false.
Switch the condition between true and false below and the lit path changes.

If (condition)?
if true
this path
if false
that path
The condition is true, so it goes left.

Whether the condition is true or false decides the path.

At a fork, it takes only one side.
If true, the true side; if false, the false side.
That's "if," and it's how a program looks at the situation and picks a path.

02

Is the age 19 or more?

A condition usually compares something.
Say the rule is that 19 or older counts as an adult. Then the question is "Is the age 19 or more?"
If the answer is true, it's an adult; if false, not yet.
Change the age below and see how the decision splits.

Ageis 19 or older?
25 ≥ 19 → true
an adult

Check whether the condition is true, then pick a path.

19 or older means adult; otherwise, not yet.
One value, the age, splits the decision.
That's how a condition compares values and produces true or false.

03

How "if" and "otherwise" fit together

"If" usually comes with a partner.
"If so, this; otherwise, that."
You write what to do when the condition is true and what to do when it's false, side by side.
This structure is what a fork looks like.

If the age is 19 or older
→ say "an adult"
Otherwise
→ say "not yet"
If the condition is true, do the top; if false, the bottom. Only one of the two.

If ... then ... otherwise ....

Write down the true case and the false case in advance, and the program picks the right one each time.
It decides by itself, without us having to watch over it.

04

That true or false is the switch at the fork

Where have we seen this true or false before?
In the last lesson on data types, and before that in the structure section, where the ALU inside the CPU answered "Is 10 greater than 7?"
That was the switch that lights up a fork in the road.

Compare (ALU)
age ≥ 19
True·false
true
Fork
to the adult path
The true·false the ALU gave back in the structure section became the switch that chooses the path here.

The true or false of a comparison → the switch at the fork.

The ALU, which calculates, makes the comparison, and its true or false becomes the switch at the fork.
Pieces we learned separately connect here.
Small things came together to become a "decision."

05

Deciding pass or fail from a score

Let's build a small program.
"If the score is 60 or more, pass; otherwise, fail."
Enter a score and run it, and it checks whether the condition is true and picks one path: pass or fail.

Scorepts
If score ≥ 60
"pass" show
Otherwise
"fail" show
The result shows when you run

Score → condition check → pass / fail.

A single score splits pass from fail.
With a condition and two branches, a program can decide for itself.
In the next lesson, we'll look at the symbols that help with calculations.

In one line"If" is a fork that chooses a path based on whether a condition is true or false: this way if true, that way if false. The true and false we saw earlier, along with the ALU's comparisons, become the switch that lets a program decide for itself.
← Programming
Was this helpful? Support seegongsik