Decision-making statements allow a Java program to execute different blocks of code depending on whether a condition is true or false.
The if statement executes a block of code
only when its condition is true.
Syntax:
if (condition) {
// Code executes if the condition is true
}
Example:
public class Main {
public static void main(String[] args) {
int age = 20;
if (age >= 18) {
System.out.println("You are eligible to vote.");
}
}
}
You are eligible to vote.
The condition age >= 18 is true, so Java
executes the statement inside the if block.
If the condition is false, Java skips the code inside
the if block.
public class Main {
public static void main(String[] args) {
int age = 15;
if (age >= 18) {
System.out.println("You are eligible to vote.");
}
System.out.println("Program finished.");
}
}
Program finished.
The first message is not displayed because the condition is false.
The if-else statement selects one of two
blocks. If the condition is true, the if
block runs; otherwise, the else block runs.
Syntax:
if (condition) {
// Executes when condition is true
} else {
// Executes when condition is false
}
Example:
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
The remainder of 7 % 2 is 1, so the
condition is false and the else block runs.
An else-if ladder checks multiple
conditions in order. The first true condition's block
executes, and the remaining conditions are skipped.
Syntax:
if (condition1) {
// First block
} else if (condition2) {
// Second block
} else if (condition3) {
// Third block
} else {
// Default block
}
Example: Checking Marks
public class Main {
public static void main(String[] args) {
int marks = 85;
if (marks >= 90) {
System.out.println("Grade A+");
} else if (marks >= 80) {
System.out.println("Grade A");
} else if (marks >= 60) {
System.out.println("Grade B");
} else if (marks >= 40) {
System.out.println("Grade C");
} else {
System.out.println("Fail");
}
}
}
Grade A
A nested if means placing one
if statement inside another
if statement.
public class Main {
public static void main(String[] args) {
int age = 20;
boolean hasID = true;
if (age >= 18) {
if (hasID) {
System.out.println("Entry allowed.");
} else {
System.out.println("Please show your ID.");
}
} else {
System.out.println("Entry not allowed.");
}
}
}
Entry allowed.
The inner if statement is checked only
after the outer condition is true.
An if-else-if ladder can classify a number
as positive, negative, or zero.
public class Main {
public static void main(String[] args) {
int number = -5;
if (number > 0) {
System.out.println("Positive number");
} else if (number < 0) {
System.out.println("Negative number");
} else {
System.out.println("Zero");
}
}
}
Negative number
Try changing number to
10 or 0 to see the other
outputs.
The remainder operator % helps determine
whether a number is even or odd.
public class Main {
public static void main(String[] args) {
int number = 12;
if (number % 2 == 0) {
System.out.println("Even number");
} else {
System.out.println("Odd number");
}
}
}
Even number
If the remainder after dividing by 2 is zero, the number is even; otherwise, it is odd.
Logical operators combine conditions inside an
if statement.
| Operator | Meaning |
|---|---|
&& |
Both conditions must be true. |
|| |
At least one condition must be true. |
! |
Reverses a Boolean condition. |
public class Main {
public static void main(String[] args) {
int age = 22;
boolean hasID = true;
if (age >= 18 && hasID) {
System.out.println("You can enter.");
} else {
System.out.println("Entry denied.");
}
}
}
You can enter.
Both conditions must be true for the
&& expression to be true.
You can use an if-else statement to
compare two numbers.
public class Main {
public static void main(String[] args) {
int a = 25;
int b = 40;
if (a > b) {
System.out.println(a + " is larger.");
} else if (b > a) {
System.out.println(b + " is larger.");
} else {
System.out.println("Both numbers are equal.");
}
}
}
40 is larger.
The final else handles the case when both
numbers are equal.
A year is a leap year if it is divisible by 400, or if it is divisible by 4 but not divisible by 100.
public class Main {
public static void main(String[] args) {
int year = 2024;
if (year % 400 == 0) {
System.out.println("Leap year");
} else if (year % 100 == 0) {
System.out.println("Not a leap year");
} else if (year % 4 == 0) {
System.out.println("Leap year");
} else {
System.out.println("Not a leap year");
}
}
}
Leap year
Try testing the program with years 2000, 1900, 2024, and 2023.
Use conditions to check whether a student has passed an examination.
public class Main {
public static void main(String[] args) {
int marks = 65;
if (marks >= 40 && marks <= 100) {
System.out.println("Pass");
} else if (marks >= 0 && marks < 40) {
System.out.println("Fail");
} else {
System.out.println("Invalid marks");
}
}
}
Pass
This example checks that marks are between 0 and 100 and treats values outside that range as invalid.
// Incorrect
// if (number = 10) { }
// Correct
if (number == 10) {
System.out.println("Number is 10");
}
if (age >= 18) {
System.out.println("Adult");
System.out.println("Eligible to apply");
}
// Incorrect order for grading
if (marks >= 40) {
System.out.println("Grade C");
} else if (marks >= 90) {
System.out.println("Grade A");
}
In this example, marks of 95 match the first condition, so the Grade A condition is never reached.
Correct order:
if (marks >= 90) {
System.out.println("Grade A");
} else if (marks >= 40) {
System.out.println("Grade C");
} else {
System.out.println("Fail");
}
This program uses if-else-if statements
to perform arithmetic operations based on an operator.
public class Main {
public static void main(String[] args) {
double a = 20;
double b = 5;
char operator = '*';
if (operator == '+') {
System.out.println("Result: " + (a + b));
} else if (operator == '-') {
System.out.println("Result: " + (a - b));
} else if (operator == '*') {
System.out.println("Result: " + (a * b));
} else if (operator == '/') {
if (b != 0) {
System.out.println("Result: " + (a / b));
} else {
System.out.println("Cannot divide by zero.");
}
} else {
System.out.println("Invalid operator.");
}
}
}
Result: 100.0
if executes code when a condition is true.if-else selects between two blocks.else-if checks multiple conditions.if statements check conditions inside other conditions.Q1. Which statement executes code only when a condition is true?
Q2. What is the output if number = 8?
if (number % 2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
Q3. Which operator means logical AND in Java?