3.1 The if-else Statement

When you have eliminated the impossible, that which remains, however improbable, must be the truth.

—SIR ARTHUR CONAN DOYLE

When you come to a fork in the road, take it.

—ATTRIBUTED TO YOGI BERRA

We begin our discussion of branching with a kind of Java statement that chooses between two alternative actions.

The Basic if-else Statement

In programs, as in everyday life, things can sometimes go in one of two different ways. If you have money in your checking account, some banks will pay you a little interest. On the other hand, if you have overdrawn your checking account, your account balance is negative, and you will be charged a penalty that will make your balance even more negative. This policy might be reflected in ...

Get Java: An Introduction to Problem Solving and Programming, 8th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.