Java If Statement - Syntax and Examples
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
if (condition) {
// code to execute if condition is true
}
The condition
must evaluate to a boolean value, either true or false. If it's true, the code inside the block executes. If not, the program simply skips that block and moves on.
Flow Diagram of If Statement
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.
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
if
block. - Writing semicolon after
if
condition:if (x > 0); { ... }
causes logic errors. That semicolon ends theif
prematurely.
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.
QUIZ
Question 1:What does the following code output?
int num = 5;
if (num < 10) {
System.out.println("Less than 10");
}
System.out.println("Done");
int num = 5;
if (num < 10) {
System.out.println("Less than 10");
}
System.out.println("Done");
Question 2:In Java, the condition inside an if
statement must always evaluate to a boolean value.
Question 3:Which of the following are best practices when using if
statements in Java?
Question 4:What happens when the following code is executed?
int age = 15;
if (age >= 18) {
System.out.println("You can vote.");
}
System.out.println("Check completed.");
int age = 15;
if (age >= 18) {
System.out.println("You can vote.");
}
System.out.println("Check completed.");
Question 5:The following code produces a logical error due to a misplaced semicolon:
int x = 10;
if (x > 5);
{
System.out.println("Greater than 5");
}
int x = 10;
if (x > 5);
{
System.out.println("Greater than 5");
}
Question 6:Review the code below. Which statements about it are true?
int marks = 85;
if (marks > 50)
System.out.println("You passed!");
System.out.println("This message is outside the if.");
int marks = 85;
if (marks > 50)
System.out.println("You passed!");
System.out.println("This message is outside the if.");
Comments
Loading comments...