Outline of a Two-way Decision
Here is how an outline of how make a two-way decision:
... statements done before the decision
if ( condition )
.... // true branch
else
.... // false branch
... statements done after the branch comes back together
Here are some details:
- The condition evaluates to true or false,
often by comparing variables and values.
- The else divides the true branch from the false branch.
- The statement after the false branch (or false block) always will be executed.
- A block consists of several statements inside a pair of braces, { and }.
- The true branch can be a block.
- The false branch can be a block.
- There can be as many statements in a block as you need.
- When a block is chosen for execution, the statements in it are executed one by one.
The condition can compare what is held in a variable to other values.
You can use the comparisons: <, >, and so on.
(More about these later.)
The first statement after the false branch will be executed no matter which
branch is chosen.
The if-else is like a fork in the road,
but the road always comes together again.
|