if Keyword Usage and Examples
Learn how to use the Java if keyword with clear examples. Understand conditional logic in Java and explore the power of decision-making using the if statement.
This reference explains if Keyword Usage and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.
Use this reference
Identify the documented behavior, read a complete example and check its inputs before applying it.
if Keyword in Java
The if keyword in Java is your first step into controlling the flow of your program. It allows your code to make decisions — a fundamental aspect of programming logic.
Whenever you want something to happen only when a specific condition is true, if is what you use.
Syntax of if Statement
if (condition) {
// code to execute if condition is true
}
The condition inside the parentheses must evaluate to a boolean value — true or false. If the result is true, the code block runs. If it's false, the block is skipped.
Example 1: Basic if Statement
public class IfExample1 {
public static void main(String[] args) {
int age = 20;
if (age >= 18) {
System.out.println("You are eligible to vote.");
}
System.out.println("Check completed.");
}
}
You are eligible to vote.
Check completed.
Explanation:
Java · example discussed here
public class IfExample1 {
public static void main(String[] args) {
int age = 20;
if (age >= 18) {
System.out.println("You are eligible to vote.");
}
System.out.println("Check completed.");
}
}
Here, the condition age >= 18 is true because age is 20. Therefore, the message inside the if block is printed. The program then continues to the next line after the block.
Example 2: Condition Fails
public class IfExample2 {
public static void main(String[] args) {
int age = 16;
if (age >= 18) {
System.out.println("You are eligible to vote.");
}
System.out.println("Check completed.");
}
}
Check completed.
Explanation:
Java · example discussed here
public class IfExample2 {
public static void main(String[] args) {
int age = 16;
if (age >= 18) {
System.out.println("You are eligible to vote.");
}
System.out.println("Check completed.");
}
}
This time, age is 16. Since age >= 18 is false, the body of the if statement is skipped. The program continues to print Check completed.
Example 3: Using Boolean Variables
public class IfExample3 {
public static void main(String[] args) {
boolean isLoggedIn = true;
if (isLoggedIn) {
System.out.println("Welcome back, user!");
}
}
}
Welcome back, user!
Notice how we used a boolean variable directly in the if condition. If it's true, the block runs. This pattern is common when checking flags or system states.
Curly Braces: Optional for Single Statements
public class IfExample4 {
public static void main(String[] args) {
int number = 10;
if (number > 5)
System.out.println("Number is greater than 5");
System.out.println("End");
}
}
Number is greater than 5
End
When there's only one statement to execute under an if condition, Java lets you skip the curly braces. But be cautious — for readability and maintainability, it's usually better to keep them.
Example 5: Nested if Statements
public class IfExample5 {
public static void main(String[] args) {
int marks = 85;
if (marks >= 50) {
if (marks >= 80) {
System.out.println("Excellent score!");
}
}
}
}
Excellent score!
Nested if statements are useful when you want to validate one condition only after another one is true. In this case, the program first checks if the score is at least 50, then checks if it’s 80 or more.
Common Mistake: Semicolon After if
if (x > 10);
System.out.println("This will always run!");
A semicolon directly after if ends the statement prematurely. The following block will run regardless of the condition — a classic beginner mistake.
Best Practices for Using if in Java
- Always use braces, even for single-line statements, to avoid logic errors.
- Keep your conditions simple and readable.
- Use boolean expressions or variables clearly — avoid nested logic when possible.
- Comment your decision branches, especially if the condition is complex.
Using this reference
Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.
Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide.
Reference checks are optional and do not count toward course progress.