Drag the edges to resize the window.

In Projects, you can keep track of your progress as you go throught the tasks. Check each item as you complete it!

Code Editor
Conditionals and Control Flow

If-Else Statement

Sometimes we execute one block of code when the Boolean expression after the if keyword is true. Other times we may want to execute a different block of code when the Boolean expression is false.

We could write a second if statement with a Boolean expression that is opposite the first, but Java provides a shortcut called the if/else conditional.

  1. The if/else conditional will run the block of code associated with the if statement if its Boolean expression evaluates to true.
  2. Otherwise, if the Boolean expression evaluates to false, it will run the block of code after the else keyword.

Here's an example of if/else syntax:

if (1 < 3 && 5 < 4) { System.out.println("I defy the Boolean laws!") } else { System.out.println("You can thank George Boole!"); }

In the example above, the Boolean expression "1 is less than 3" and "5 is less than 4" evaluates to false. The code within the if block will be skipped and the code inside the else block will run instead. The text "You can thank George Boole!" will be printed in the console.

Report a Bug
If you see a bug or any other issue with this page, please report it here.