seegongsik
Saved words
Programming

If so, then a fork

Last time we saw true and false. With that true·false a program chooses a path. If the condition holds, this path, if not, that path. It's the first moment a program judges on its own.

01

True goes this way, false goes that way

A program sometimes
meets a fork in the road.
"If so, do this,
otherwise do that."
By whether the condition is true or false,
the path it takes changes.
Flip the condition
and watch which path lights up.

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

The condition's true·false decides the path.

At the fork
it takes only one side.
True, the true side,
false, the false side.
This is "if."
The program looks at the situation
and chooses a path.

02

Is the age 19 or older?

A condition usually
compares something.
"Is the age 19 or older?"
a question like that.
If the answer is true, an adult,
if false, not yet.
Change the age
and watch how the judgment splits.

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

It checks if the condition is true and chooses a path.

19 or older, an adult,
otherwise, not yet.
By one value, the age,
the judgment splits.
This is how a condition
compares a value
and gives true or false.

03

The shape of "if" and "otherwise"

"If" usually
has a partner.
"If so, this,
otherwise, that."
What to do when the condition is true,
and when it's false,
you write them side by side.
This shape 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, each time,
chooses the right side.
Even without us
watching every step,
it judges on its own.

04

That true·false is the fork's switch

This true·false,
where have we seen it?
In data types last time,
and even earlier
when the CPU's ALU
answered "is 10 bigger than 7"
with that true·false.
That was exactly
the switch that turns the fork.

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

Comparison's true·false → the fork's switch.

The calculating ALU
does a comparison,
and that true·false
becomes the fork's switch.
The pieces we learned one by one
connect here.
Small things gathered
into "judgment."

05

Judging a pass by score

Let's make a small program.
"If the score is 60 or higher, pass,
otherwise, fail."
Put in a score
and run it,
and it checks if the condition is true
and chooses the path,
pass or fail.

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

Score → condition check → pass / fail.

By one score
pass and fail split.
With just a condition and two branches,
a program can judge
on its own.
Next time we'll look at
the many symbols
that help with calculation.

In one line"If" is a fork that chooses a path by a condition's true or false. True, this path; false, that path. The true·false we saw last time, and the ALU's comparison, became here the switch a program judges with on its own.
Was this helpful? Support seegongsik
Programming