⏱️ 7 min read • Beginner Level • Lesson 22
Completed on . You can revise this lesson or continue to the next topic.
The if-else statement in Java is used when we want to execute
one block of code if a condition is true, and another block of code
if the condition is false.
Before learning the if-else statement, you should understand
Java if statement,
Java operators, and
Java data types.
The if-else statement is a decision-making statement.
It checks a condition and chooses between two possible blocks of code.
If the condition is true, the if block executes.
If the condition is false, the else block executes.
It is useful when exactly one of two actions should happen.
The syntax of an if-else statement contains one condition, one if block,
and one else block.
if (condition) {
// statements execute if condition is true
} else {
// statements execute if condition is false
}
if must return a boolean value:
either true or false.
The flowchart below shows how an if-else statement works. Java checks the condition first. If the condition is true, the if block executes. Otherwise, the else block executes.
if statement.
true, the statements inside the
if block are executed.
false, the statements inside the
else block are executed.
if block or the else block,
the program continues with the next statement after the if-else structure.
The following program checks whether a number is positive or negative using an
if-else statement. If the entered number is greater than or equal to
0, the program prints "X is Positive". Otherwise, it prints
"X is Negative".
import java.util.Scanner;
public class IfElseExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter any number: ");
int x = sc.nextInt();
if (x >= 0) {
System.out.println("X is Positive");
} else {
System.out.println("X is Negative");
}
sc.close();
}
}
Output 1:
Enter any number:10 X is Positive
Output 2:
Enter any number:-12 X is Negative
Scanner class from the
java.util package.
Scanner object named sc is created to read input
from the keyboard.
System.out.print("Enter any number: "); displays
a message asking the user to enter a number.
int x = sc.nextInt(); reads the integer value
entered by the user and stores it in the variable x.
x >= 0 checks whether the value of
x is greater than or equal to zero.
true, the if block executes
and prints "X is Positive".
false, the else block executes
and prints "X is Negative".
sc.close(); closes the Scanner object and releases
system resources.
12, the condition 12 >= 0
becomes true, so the program prints X is Positive.
-12, the condition -12 >= 0
becomes false, so the program prints X is Negative.
if-else statement, only one block executes:
either the if block or the else block.
The following program checks whether a student passed or failed based on marks.
If the marks are greater than or equal to 33, the program prints
"Pass". Otherwise, it prints "Fail".
public class PassFailExample {
public static void main(String[] args) {
int marks = 25;
if (marks >= 33) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
}
}
Output:
Fail
marks is declared and initialized with the value
25.
marks >= 33 checks whether the student has scored
at least 33 marks.
25 >= 33 is false, the if
block is skipped.
else block executes and prints "Fail".
marks was 33 or more, the program
would print "Pass".
In this example, we will check whether a person is eligible to vote or not.
If the age is greater than or equal to 18, the person is eligible to vote.
Otherwise, the person is not eligible to vote.
public class VotingEligibilityExample {
public static void main(String[] args) {
int age = 20;
if (age >= 18) {
System.out.println("Eligible to Vote");
} else {
System.out.println("Not Eligible to Vote");
}
}
}
Output:
Eligible to Vote
age is initialized with the value 20.
age >= 18 checks whether the age is greater than or equal to 18.
20 >= 18 is true, the if block executes.
Eligible to Vote.
age was less than 18, the else block would execute and print Not Eligible to Vote.
| if Statement | if-else Statement |
|---|---|
| Executes a block only if the condition is true. | Executes one block if true and another block if false. |
| No alternate block is required. | Alternate else block is available. |
| Used when action is needed only for true condition. | Used when one of two actions must happen. |
= instead of == for comparison.if condition.else with a separate condition. The else block does not have a condition.public class IfElseSemicolonMistake {
public static void main(String[] args) {
int x = -10;
if (x >= 0); {
System.out.println("Positive");
} else {
System.out.println("Negative");
}
}
}
if statement before the block.
Avoid placing a semicolon after if (condition).
if-else statement is used for two-way decision-making.if block executes.else block executes.if must be a boolean expression.{ } for better readability and safer code.🧠 Test your understanding with a quick quiz
Topic: If-else-statement | Language: Java
int marks = 25;
if (marks >= 33) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}int x = -12;
if (x >= 0) {
System.out.println("Positive");
} else {
System.out.println("Negative");
}int x = 12;
if (x >= 0) {
System.out.println("Positive");
} else {
System.out.println("Negative");
}int x = 10;
if (x = 10) {
System.out.println("Equal");
} else {
System.out.println("Not Equal");
}🎉 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.