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.
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.
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.
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.
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.
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 ~ 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.
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.
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."
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.
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.