⬅ Previous TopicJava If Statement
Next Topic ⮕Java if-else-if Statement





- 1Java OOP Introduction
- 2Java Class
- 3Java Class Constructor
- 4Java Class Objects
- 5Java Access Modifiers
- 6Java Static Variables in Classes
- 7Java Static Methods Explained
- 8Java Static Blocks
- 9Java final Variables
- 10Java final Methods
- 11Java final class
- 12Inheritance in Java
- 13Java Method Overriding
- 14Java Abstraction in OOP
- 15Interfaces in Java
- 16Polymorphism in Java
- 17Encapsulation in Java
- 18Java Nested Classes
- 19Java Nested Static Class
- 20Java Anonymous Class
- 21Java Singleton Class
- 22Java Enums
- 23Reflection in Java



Java if-else Statement - Syntax and Examples
if-else Statement
In Java, the if-else
statement is used to make decisions in your program. It checks a condition and runs different code depending on whether the condition is true or false.
For example: if it rains, you stay home; else, you go outside. That's how if-else
works.
Syntax of if-else Statement
if (condition) {
// Runs if condition is true
} else {
// Runs if condition is false
}
Basic Example: Check if a Number is Positive
public class Main {
public static void main(String[] args) {
int number = 10;
if (number > 0) {
System.out.println("The number is positive.");
}
}
}
The number is positive.
Example with else: Even or Odd
public class Main {
public static void main(String[] args) {
int number = 7;
if (number % 2 == 0) {
System.out.println("Even number");
} else {
System.out.println("Odd number");
}
}
}
Odd number
Nested if Statements
Sometimes you want to check another condition only if the first one passes. That’s when nesting helps.
public class Main {
public static void main(String[] args) {
int age = 20;
int score = 85;
if (age >= 18) {
if (score > 80) {
System.out.println("Eligible for scholarship.");
} else {
System.out.println("Eligible, but no scholarship.");
}
} else {
System.out.println("Not eligible.");
}
}
}
Eligible for scholarship.
Key Takeaways
if
checks a condition and runs code when it’s true.else
provides an alternate path when theif
fails.if-else-if
allows multiple conditions to be checked in order.- Nested
if
helps for dependent checks.
Real-world Example: Login Attempt
public class Main {
public static void main(String[] args) {
String username = "admin";
String password = "1234859";
if (username.equals("admin")) {
if (password.equals("1234")) {
System.out.println("Login successful!");
} else {
System.out.println("Incorrect password.");
}
} else {
System.out.println("Username not found.");
}
}
}
Login successful!
QUIZ
Question 1:What is the role of the else
block in an if-else
statement in Java?
Question 2:Nested if
statements in Java allow you to check a second condition only if the first one is true.
Question 3:Which of the following are true about if-else
in Java?
Question 4:What is the output of the following code?
int number = 7;
if (number % 2 == 0) {
System.out.println("Even number");
} else {
System.out.println("Odd number");
}
int number = 7;
if (number % 2 == 0) {
System.out.println("Even number");
} else {
System.out.println("Odd number");
}
Question 5:In Java, the if
block only executes when the condition evaluates to true
.
Question 6:Consider this snippet:
int age = 20;
int score = 85;
if (age >= 18) {
if (score > 80) {
System.out.println("Eligible for scholarship.");
} else {
System.out.println("Eligible, but no scholarship.");
}
} else {
System.out.println("Not eligible.");
}
What will be the output?
int age = 20;
int score = 85;
if (age >= 18) {
if (score > 80) {
System.out.println("Eligible for scholarship.");
} else {
System.out.println("Eligible, but no scholarship.");
}
} else {
System.out.println("Not eligible.");
}