If
If
A school program needs reliable data and clear rules. An if statement runs its block only when a boolean condition is true. Use it when a school action is optional, such as congratulating a passing student.
Understand
An if statement runs its block only when a boolean condition is true.
Apply
Trace the Java example, predict its output and explain the result before running it.
Avoid mistakes
The statement after the block runs regardless of the condition. Braces make the intended boundary visible.
If Statement in Java
The if statement in Java is like a gate. It opens only if a certain condition is true. Otherwise, it quietly ignores the code block inside it. This simple control structure forms the basis for decision-making in Java programs.
Syntax of If Statement in Java
Flow Diagram of If Statement
Flow diagram: key actions and decisions
- start
- condition?
- if-code-block
- end
These labels identify the actions and decisions in the original diagram; the list does not represent branch order. Follow the Java code for the exact execution path.
Example 1: Student Test Score
Use case: A software engineer is developing a system that checks a student's test score. If the score is greater than 5, the system should acknowledge that the student has passed the minimum benchmark.
Flow diagram: key actions and decisions
- start
- Initialize score
- Is score > 5?
- Print score is greater than 5
- Print Program ended.
- end
These labels identify the actions and decisions in the original diagram; the list does not represent branch order. Follow the Java code for the exact execution path.
Program description: In this Java program, we simulate a scenario where a student's test score is stored in the variable x. Let us assume that the value in x is set to 10 for this run. In real-time, we may get the value for x as input from the user. The if statement checks whether the score is greater than 5. If this condition is true, the program prints a message indicating that the score is above the benchmark. Regardless of the condition, the program ends with a general message indicating completion.
public class IfExample1 {
public static void main(String[] args) {
int score = 10;
if (score > 5) {
System.out.println("score is greater than 5");
}
System.out.println("Program ended.");
}
}
score is greater than 5
Program ended.
Explanation:
Since x > 5 evaluates to true, the message inside the if block is printed. The final statement runs regardless of the condition.
Example 2: Condition That Fails
public class IfExample2 {
public static void main(String[] args) {
int age = 15;
if (age >= 18) {
System.out.println("You can vote.");
}
System.out.println("Check completed.");
}
}
Check completed.
Explanation:
Here, the condition age >= 18 is false. So, the code inside the if block is ignored. Only the final line runs.
Best Practices When Using If Statements
- Always ensure your condition is boolean: Non-boolean conditions won't compile in Java.
- Use braces
{ }even for single-line blocks: It improves readability and helps avoid logical bugs. - Keep your conditions clear and readable: Use meaningful variable names and avoid overly complex expressions.
Common Mistakes to Avoid
- Using
=instead of==: Assignment vs. comparison.if (a = 10)is invalid. Use==for comparison. - Forgetting curly braces: Without them, only the next line is considered part of the
ifblock. - Writing semicolon after
ifcondition:if (x > 0); { ... }causes logic errors. That semicolon ends theifprematurely.
Example 3: No Curly Braces – What Happens?
public class IfExample3 {
public static void main(String[] args) {
int marks = 45;
if (marks > 50)
System.out.println("You passed!");
System.out.println("This message is outside the if.");
}
}
This message is outside the if.
Explanation:
Only the first print statement is inside the if. The second line executes every time. To avoid such surprises, always use curly braces.
Summary
The Java if statement allows your programs to make decisions. Always test your conditions, keep your code readable, and avoid syntax pitfalls. As you progress, if will serve as a foundation for more advanced constructs like else, else if, and switch statements.
Important boundaries and mistakes
The statement after the block runs regardless of the condition. Braces make the intended boundary visible.
Try the school example
Before running this Java program, write down each line you expect to see. Identify the value, branch, object or exception responsible for each line. Then explain whether the following assumption is valid: The statement after the block runs regardless of the condition.
Java 17 · Main.java · complete practice program
public class Main {
public static void main(String[] args) {
int marks = 72;
if (marks >= 50) {
System.out.println("Pass");
}
System.out.println("Report ready");
}
}
Now change one thing: Change marks to 40. Predict the new behavior before checking the worked variation.
Worked answer and reasoning
Java 17 · Main.java · source repeated for this answer
public class Main {
public static void main(String[] args) {
int marks = 72;
if (marks >= 50) {
System.out.println("Pass");
}
System.out.println("Report ready");
}
}
Expected output
Pass
Report readyAn if statement runs its block only when a boolean condition is true. Use it when a school action is optional, such as congratulating a passing student.
The statement after the block runs regardless of the condition. Braces make the intended boundary visible.
Run with javac Main.java, then java Main. The declaration of Main names the class; main starts the example, and println writes a complete output line.
Worked variation: Change marks to 40. The Pass line is skipped, but Report ready is still printed.
A passing mark follows one path
Store the mark
The local variable marks receives the integer 72.
Evaluate the condition
72 >= 50 is true, so execution enters the if block.
Print inside the block
The selected block prints Pass.
PassContinue after the block
Report ready is outside the if block and is printed after it finishes.
Pass Report ready
Check your understanding
Check a common mistake
What to remember
An if statement runs its block only when a boolean condition is true. Use it when a school action is optional, such as congratulating a passing student. The statement after the block runs regardless of the condition. Braces make the intended boundary visible.
Related explanations: Java Variables, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide and Java 17 language specification.
Reaching the recap and completing the quizzes are separate milestones.