if-else-java
Conditional statements are essential in Java for controlling the flow of your program based on specific conditions. The if-else construct is one of the most commonly used conditional structures, allowing your code to make decisions and execute different blocks depending on whether a condition is true or false.
What is if-else?
The if-else statement lets you execute a block of code if a condition is true, and a different block if the condition is false.
Syntax:
The if-else statement lets you execute a block of code if a condition is true, and a different block if the condition is false.
Syntax:
if (condition) { // Executes if condition is true} else { // Executes if condition is false}
Types of if Statements in Java
Java offers several variations of the if statement to handle different decision-making scenarios:
Output:
Output:
Output:
Output:
Use Case for if, if-else, if-else-if ladder and Nested if Table
Java offers several variations of the if statement to handle different decision-making scenarios:
1. Simple if Statement
Executes a block of code only if the specified condition is true.
Example:
Executes a block of code only if the specified condition is true.
Example:
public class IfStatement { public static void main(String[] args) { int a = 10, b = 20; if (a < b) { System.out.println("True"); } }}
Output:
True
2. if-else Statement
Executes one block if the condition is true, and another block if it is false.
Example:
Executes one block if the condition is true, and another block if it is false.
Example:
public class IfElseStatement { public static void main(String[] args) { int a = 10, b = 20; if (a > b) { System.out.println("True"); } else { System.out.println("False"); } }}
Output:
False
3. if-else-if Ladder
Allows you to check multiple conditions in sequence. The first true condition’s block is executed; if none are true, the else block runs.
Example:
Allows you to check multiple conditions in sequence. The first true condition’s block is executed; if none are true, the else block runs.
Example:
public class IfElseIfStatement { public static void main(String[] args) { int a = 10, b = 20, c = 30; if (a > b) { System.out.println("a is greater than b"); } else if (b < c) { System.out.println("b is less than c"); } else { System.out.println("No condition matched"); } }}
Output:
b is less than c
4. Nested if Statement
Places one if statement inside another, enabling you to check multiple related conditions.
Example:
Places one if statement inside another, enabling you to check multiple related conditions.
Example:
public class NestedIfStatement { public static void main(String[] args) { int age = 61; if (age >= 18) { if (age >= 18 && age <= 60) { System.out.println("You are Eligible for Driving License"); } else { System.out.println("You are Not Eligible for Driving License"); } } else { System.out.println("You are Not Eligible for Driving License"); } }}
Output:
You are Not Eligible for Driving License
Key Points
- The if statement checks a condition and executes code only if the condition is true.
- The if-else structure provides an alternative path if the condition is false.
- The if-else-if ladder is useful for multiple conditions.
- Nested if statements help handle complex, related checks.
Use Case for if, if-else, if-else-if ladder and Nested if Table
Statement Type | Use Case |
---|---|
if | Single condition, single action |
if-else | Two possible actions (true / false ) |
if-else-if ladder | Multiple conditions, multiple actions |
Nested if | Complex conditions, hierarchical checking |
Tips: Use clear and meaningful conditions to make your code easy to read and maintain. Proper indentation and structure help others (and yourself) understand the logic at a glance.