if Statements

If you want to test a condition in a Java program, the most basic way is with an if statement. As you learned previously, the boolean variable type is used to store only two possible values: true or false. The if statement works along the same lines, testing to see whether a condition is true or false, and taking action only if the condition is true.

You use if along with a condition to test, as in the following statement:

if (account < 0)
    System.out.println("Hear that bouncing noise? It's your checks");

Although this code is listed on two lines, it's one statement. The first part uses if to determine whether the account variable is less than 0 by using the < operator. The second part displays the text Hear that bouncing noise? ...

Get SAMS Teach Yourself Programming with Java™ in 24 Hours, FOURTH 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.