Java if and else
Until now you stored true or false, or you built one true or false from two others with marks like &&. Those pages still ended with one value sitting under a name. This page uses that value for something else: Java looks at true or false and runs only one group of lines. The tool for that choice is if. The other group, when you need it, is else.
A boolean decides which block runs
Create a small program file named Gate.java. The public class on the first line must match the file name: public class Gate. Do not keep an older Mint class in this file; the compiler expects the class name and the file name to agree.
Inside main, store a boolean before you choose a path:
boolean ready = true;
That line does not print anything. It only gives the name ready the word true. ready starts with a small r. There are no quotes around true. The semicolon ends the storing line. After that comes the choice itself:
if (ready) {
System.out.println("pass");
} else {
System.out.println("wait");
}
The word if is lowercase: i, then f. It is not a name you invent. A space after if is fine; if(ready) without a space also compiles. Inside the parentheses sits the name ready — no quotes, no ==, no &&. When ready holds true, Java enters the first pair of braces and runs System.out.println("pass");. The quotes around pass mark letters for the screen. Those letters — p, a, s, s — are what you should see, one line. The else block is skipped entirely on this run. Save the file before you compile.
Program that prints pass
This is the version you compile first.
public class Gate {
public static void main(String[] args) {
boolean ready = true;
if (ready) {
System.out.println("pass");
} else {
System.out.println("wait");
}
}
}
From the folder that holds Gate.java, run javac Gate.java. When the braces and parentheses match, javac writes no line of its own. Then run java Gate — not java Gate.java. Leave the .java ending off the second command. The same two commands work on Windows and on macOS; this page does not depend on a different shell spelling for either system. The program should print pass and stop. It should not ask you to type. If the terminal sits waiting for keyboard input, a nextLine call from an older exercise is still in the file. Delete that call and keep the boolean plus the if / else above. You do not need a new JDK install for a wrong screen line.
Why pass and not wait? Because the test in parentheses was true. Java does not print the word true on this run. It prints whatever println runs inside the chosen braces. Think of three jobs: parentheses hold the test, braces group the lines that belong together, and println is the only part that writes to the screen. If the screen shows true, you are probably still printing the boolean itself, or an older combining-mark demo is still in main. Replace that writing line with println("pass") inside the if braces when ready is true.
When ready is false
Change only the stored value:
boolean ready = false;
Leave the if / else structure as it is. Now the test fails, so Java skips the first braces and runs the else braces instead. The screen should show wait, one line.
public class Gate {
public static void main(String[] args) {
boolean ready = false;
if (ready) {
System.out.println("pass");
} else {
System.out.println("wait");
}
}
}
Compile and run again: javac Gate.java, then java Gate. You should see wait. Notice that you did not rewrite the if line or the else line — only the value under ready changed. After you have seen wait, you can put boolean ready = true; back to return to the first demo. Both versions are valid Java. They teach the same if / else shape with opposite tests.
Read the false run once more in order. ready holds false. if (ready) is asked. The answer is false, so every line between the first { and its matching } is skipped — including println("pass"). Control continues at else. The lines between the else braces run, so println("wait") executes. One line on the screen. No second line. The word false itself never appears unless you print it on purpose.
Rules that keep the choice clear
Parentheses around the test are required. Writing if ready { … } without parentheses makes javac fail. The test on this page is a boolean: a name like ready, or the bare words true or false with no quotes. if ("true") puts letters where a boolean test belongs; javac reports an error. Remove the quotes, or keep using a boolean name. A whole number alone such as if (1) is not how this lesson stores readiness; stay with true, false, or a boolean name.
Braces matter for reading the lesson. This page always wraps each path in { }. Java allows a one-line if without braces, but the moment you add a second line under that if, only the first line stays tied to the test unless braces are there. Beginners lose that detail easily. Keep braces in both demos so each path is obvious: one opening brace after the test, one closing brace before else, then another pair for else.
else belongs to an if. A lone else makes javac fail. On this page one if has one else. You may write if without else when you only care about the true path; the first teaching demo still shows both sides so you can see the fork. Choosing among many fixed values is switch on a later page — do not invent a third branch with another else here.
You can also write the test without a separate name: if (true) { … } else { … }. That compiles and runs. The demos use ready so you see a stored boolean driving the choice, which is how programs look after you already know how to store true and false. Comparisons such as if (score > 10) belong after you are comfortable with this true / false fork; this page keeps the test as a boolean value you already understand.
📊 ready and the screen
| ready | Screen |
|---|---|
| true | pass |
| false | wait |
Use the table as a check after a run, not instead of one. true with the first demo prints pass. false with the second demo prints wait. If both println lines somehow run, the braces or the if / else pairing is wrong — fix the structure rather than reinstalling Java.
A common mix-up is printing the syntax itself: System.out.println("if (ready)");. That writes the characters if (ready). It does not choose a path. Another mix-up is leaving && or == demos in the same main and wondering why the screen shows true instead of pass. This session’s main should hold the boolean and the if / else only.
What you can do now
You can store a boolean, put it in if (…), and give each outcome its own braced block. When the test is true, one println runs; when it is false, the else println runs. The next page is switch, where several fixed values each get their own case instead of a single true / false fork.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top