If Details
layout: default
title: if Syntax
if ( expression )
statement;
if is the required keyword
* () follow if, and the expression inside must evaluate to a boolean
* The next statement after the () will execute if the expression is true.
Notice how the "controlled statement" is indented from the if.
* This is not required, but is very important for readability. Do it.
Practice Exercise¶
If you do not indent correctly, such as indenting an if's statement, other programmers will think you don't know what you're doing. Get in the habit of indenting.
if ( answer != 42 )
System.out.println("Wrong answer ...");
We may draw a flow chart to represent this logic, with the condition in a decision diamond.

Practice Exercise¶
The first statement after an if (expression) is the one to execute. Look at this code.
boolean isBuggy = false;
if (isBuggy);
System.out.println("Yes, there are bugs");
; after the if statement. Since ; is the first statement* after the if, it is the one that executes. "Yes, there are bugs" will always print because it is not part of the if.
* ; is the shortest statement in Java.
Practice Exercise¶
The code below will not compile.
int x = 50;
int y;
if (x < 100){
y = 99;
}
System.out.println("y is " + y); //Compiler error
if statement's body may not execute. Because x is a variable, and variables could change, the compiler thinks there is a chance y will not be initialized. Trying to use a variable before it is initialized causes a compiler error.