Conditional Statements in Java
![]() |
Java - Conditional Statements |
Conditional statements allow us to control the flow of execution based on specific conditions. In this guide, we will explore key concepts such as if
, else
, else if
, and nested conditions.
1. If...Else Statement
The if...else
statement executes a block of code when a specified condition is met; otherwise, the else
block runs.
Example:
A vending machine dispenses items based on token input.
class VendingMachine {
public static void main(String[] args) {
int token = 20;
if (token == 20) {
System.out.println("Collect Water Bottle");
} else {
System.out.println("Invalid Token");
}
}
}
If the token is not 20, the message "Invalid Token" is displayed.
2. Else If Statement :
The else if
statement allows multiple conditions to be checked sequentially. If no conditions are met, the else
block executes.
Example:
The vending machine now supports multiple items.
class VendingMachine {
public static void main(String[] args) {
int token = 30;
if (token == 10) {
System.out.println("Collect Chips");
} else if (token == 20) {
System.out.println("Collect Water Bottle");
} else if (token == 30) {
System.out.println("Collect Soft Drink");
} else {
System.out.println("Invalid Token");
}
}
}
true
.3. Nested Conditions
Conditional statements can be nested within each other to handle complex logic.
Example:
Selecting a soft drink based on token input.
class VendingMachine {
public static void main(String[] args) {
int token = 30;
char softDrink = 'S';
if (token == 30) {
if (softDrink == 'S') {
System.out.println("Collect Sprite");
} else if (softDrink == 'F') {
System.out.println("Collect Fanta");
} else {
System.out.println("Invalid Option");
}
} else {
System.out.println("Invalid Token");
}
}
}
Here, if the token is valid, a second condition determines the specific drink.
4. Optional Else Statement
The else
block is optional; if omitted, nothing executes when conditions are unmet.
Example:
class VendingMachine {
public static void main(String[] args) {
int token = 30;
char softDrink = 'S';
if (token == 30) {
if (softDrink == 'S') {
System.out.println("Collect Sprite");
} else if (softDrink == 'F') {
System.out.println("Collect Fanta");
}
}
}
}
Without an else
block, no message appears if conditions are unmet.
5. Common Mistakes :
Misplacing Else Statement :
The else
statement must follow an if
block without interruptions.
Incorrect:
if (false) {
System.out.println("If Block");
}
System.out.println("After If");
else { // ❌ Error: 'else' without 'if'
System.out.println("Else Block");
}
Correct :
if (false) {
System.out.println("If Block");
} else {
System.out.println("Else Block");
}
System.out.println("After Else");
Output :
Else Block
After Else
Incorrect Else If Order :
The else if
block must come before the else
block.
Incorrect:
if (token == 10) {
System.out.println("Collect Chips");
} else if (token == 20) {
System.out.println("Collect Water Bottle");
} else {
System.out.println("Invalid Token");
} else if (token == 30) { // ❌ Error
System.out.println("Collect Soft Drink");
}
Correct :
if (token == 10) {
System.out.println("Collect Chips");
} else if (token == 20) {
System.out.println("Collect Water Bottle");
} else if (token == 30) {
System.out.println("Collect Soft Drink");
} else {
System.out.println("Invalid Token");
}
Output:
Collect Water Bottle
6. If...Else Without Curly Braces
Curly braces {}
can be omitted when the block contains only one statement.
Example:
class VendingMachine {
public static void main(String[] args) {
int token = 20;
if (token == 20) System.out.println("Collect Water Bottle");
else System.out.println("Invalid Token");
}
}
Summary
- The
if...else
statement executes code based on a condition. - The
else if
statement allows multiple conditions. - Nested conditions handle complex scenarios.
- The
else
statement is optional. - The order of
if
,else if
, andelse
must be correct. - Curly braces can be omitted for single-statement blocks.
By mastering these concepts, you can effectively control program flow in Java applications.