⏱️ 7 min read • Beginner Level • Lesson 21
Completed on . You can revise this lesson or continue to the next topic.
The if statement in Java is used to execute a block of code
only when a specified condition is true. It is one of the most basic
and important decision-making statements in Java programming.
Before learning the if statement, you should understand
Java operators,
Java data types, and
Java variables.
Many times, we want a set of instructions to execute only in a particular situation.
For example, if a student gets marks greater than or equal to 33, we may want to print
"Pass". Otherwise, we may print something else.
This kind of logic is handled using decision control statements.
In decision-making, Java checks a condition and executes code based on whether the
condition evaluates to true or false.
Decision-making statements help programs choose different paths.
Conditions usually use relational and logical operators such as
>, <, ==, &&, and ||.
If the condition is true, the statement block executes.
If it is false, the block is skipped.
Java supports multiple decision-making statements. Each one is used for a different type of condition-based logic.
| Statement | Use | Learn More |
|---|---|---|
| Simple if Statement | Executes code only when condition is true. | Current Topic |
| if-else Statement | Executes one block if true and another block if false. | Read |
| if-else-if Ladder | Checks multiple conditions one by one. | Read |
| Nested if Statement | Places one if statement inside another if statement. | Read |
| switch-case Statement | Selects one block from multiple options. | Read |
The if statement checks a boolean condition. If the condition is
true, the code inside the if block is executed.
If the condition is false, the block is skipped.
if must produce a boolean value.if (condition) {
// statements execute only if condition is true
}
a < 20, marks >= 33, or isValid == true.
public class IfExample {
public static void main(String[] args) {
int marks = 70;
if(marks >= 33) {
System.out.println("Pass");
}
}
} Output: Pass
The above code prints Pass if the marks are greater than or equal to 33.
The following diagram shows how an if statement works.
Java first checks the condition. If it is true, the if block executes.
Otherwise, the block is skipped.
Execution flow of Java if statement
The following program checks whether the value of a is less than 20.
Since the condition is true, the message inside the if block is printed.
public class IfStatementExample {
public static void main(String[] args) {
int a = 10;
if (a < 20) {
System.out.println("a is less than 20");
}
System.out.println("value of a is: " + a);
}
}
Output:
a is less than 20 value of a is: 10
IfStatementExample.main() method is the entry point of the program.a is initialized with value 10.a < 20 is checked.10 < 20 is true, the message inside the if block is printed.a.Here is another simple example that checks whether a student has passed based on marks.
public class PassCheckExample {
public static void main(String[] args) {
int marks = 75;
if (marks >= 33) {
System.out.println("You passed the exam.");
}
System.out.println("Program completed.");
}
}
Output:
You passed the exam. Program completed.
= instead of comparison operator ==.
{ } when multiple statements should be inside the if block.
if condition.
public class IfSemicolonMistake {
public static void main(String[] args) {
int marks = 20;
if (marks >= 33); {
System.out.println("This line runs because of the unwanted semicolon.");
}
}
}
if condition. It ends the if statement early and can produce unexpected output.
if statement is used for decision-making in Java.if must evaluate to true or false.{ } for better readability and safer code.🧠 Test your understanding with a quick quiz
Topic: If-statement | Language: Java
int number = 50;
if (number > 10);
{
System.out.println("Inside block");
}int marks = 25;
if (marks >= 33) {
System.out.println("Pass");
}
System.out.println("Result checked");int a = 10;
if (a < 20) {
System.out.println("a is less than 20");
}
System.out.println("Done");int x = 10;
if (x = 10) {
System.out.println("Hello");
}🎉 Great job! Continue learning Java step by step.
Mark this lesson as completed to track your learning progress. To keep track across devices, please login and save your learning progress.
Discussion
Ask questions, share suggestions, or discuss this lesson.