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. Next follows a made up military situation that is kind of game-like in its 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 bridge while awaiting reinforcements.
The first command the captain wants to make sure his troops understand is this:
If they come over the bridge, shoot them.
So how do we simulate this situation in Java? We need a Boolean variable isComingOverBridge...