If they come over the bridge, shoot them
As we saw in the previous chapter, operators are used in determining whether and how often a loop should execute the code in its body.
We can now take things a step further. Let's look at putting the most common operator, ==
, to use with the Java if
and else
keywords. Then, we can start to see the powerful yet fine control that they offer us.
We will use if
and a few conditional operators, along with a small story, to demonstrate their use. The following diagram illustrates how we can devise conditions to determine whether a block of code should execute:
The following is a made-up military situation that is kind of game-like in nature.
The captain is dying and, knowing that his remaining subordinates are not very experienced, he decides to write a Java program to convey his last orders after he has died. The troops must hold one side of a...