The else
keyword in Java is used in conjunction with the if
keyword to create conditional statements that specify an alternative block of code to execute if the if
condition is false.
Table of Contents
- Introduction
else
Keyword Syntax- Understanding
else
- Examples
- Basic Usage
- Using
if-else
- Using
if-else if-else
- Real-World Use Case
- Conclusion
Introduction
The else
keyword allows you to specify a block of code that will be executed if the if
condition is false. This provides a way to handle both the true and false outcomes of a condition.
else Keyword Syntax
The syntax for the else
statement is as follows:
if (condition) { // code to be executed if the condition is true } else { // code to be executed if the condition is false }
Example:
if (age >= 18) { System.out.println("You are an adult."); } else { System.out.println("You are not an adult."); }
Understanding else
The else
statement is used to define a block of code that will be executed if the if
condition evaluates to false. This helps in handling cases where the condition is not met.
Key Points:
- The
else
block is optional and follows anif
block. - Only one
else
block can follow anif
block.
Examples
Basic Usage
To demonstrate the basic usage of the else
keyword, we will check if a number is positive or not.
Example
public class ElseExample { public static void main(String[] args) { int number = -10; if (number > 0) { System.out.println("The number is positive."); } else { System.out.println("The number is not positive."); } } }
Output:
The number is not positive.
Using if-else
The if-else
statement allows you to execute one block of code if the condition is true and another block of code if the condition is false.
Example
public class IfElseExample { public static void main(String[] args) { int temperature = 30; if (temperature > 20) { System.out.println("It is warm outside."); } else { System.out.println("It is cold outside."); } } }
Output:
It is warm outside.
Using if-else if-else
The if-else if-else
statement allows you to check multiple conditions and execute different blocks of code based on which condition is true.
Example
public class IfElseIfExample { public static void main(String[] args) { int score = 85; if (score >= 90) { System.out.println("Grade: A"); } else if (score >= 80) { System.out.println("Grade: B"); } else if (score >= 70) { System.out.println("Grade: C"); } else if (score >= 60) { System.out.println("Grade: D"); } else { System.out.println("Grade: F"); } } }
Output:
Grade: B
Real-World Use Case
Checking User Input
In real-world applications, the else
keyword is useful for handling user input and providing feedback based on the input.
Example
public class UserAgeCheck { public static void main(String[] args) { int age = 15; if (age >= 18) { System.out.println("You are eligible to vote."); } else { System.out.println("You are not eligible to vote."); } } }
Output:
You are not eligible to vote.
Conclusion
The else
keyword in Java is an essential part of creating conditional statements. It allows you to specify an alternative block of code to execute when the if
condition is false. By understanding and using the else
keyword, you can effectively control the flow of your Java programs and handle different conditions more gracefully.