Welcome to The Coding College! In this tutorial, we will discuss the if … else statement in Java, which is one of the fundamental control flow mechanisms. This structure enables your program to make decisions based on conditions and execute specific blocks of code accordingly.
What is an If … Else Statement?
The if … else statement allows you to run different code blocks based on whether a condition is true or false.
if
: Executes a block of code if the condition evaluates to true.else
: Executes an alternate block of code if the condition evaluates to false.
Syntax
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
Example: Basic If … Else
public class IfElseExample {
public static void main(String[] args) {
int age = 18;
if (age >= 18) {
System.out.println("You are eligible to vote.");
} else {
System.out.println("You are not eligible to vote.");
}
}
}
Output:
You are eligible to vote.
If … Else Ladder
You can chain multiple if
and else if
statements to evaluate multiple conditions.
Syntax:
if (condition1) {
// Code for condition1
} else if (condition2) {
// Code for condition2
} else {
// Code if none of the conditions are true
}
Example:
public class IfElseLadder {
public static void main(String[] args) {
int marks = 85;
if (marks >= 90) {
System.out.println("Grade: A+");
} else if (marks >= 75) {
System.out.println("Grade: A");
} else if (marks >= 60) {
System.out.println("Grade: B");
} else {
System.out.println("Grade: C");
}
}
}
Output:
Grade: A
Nested If … Else
An if
or else
block can contain another if ... else
statement. This is known as a nested if … else.
Example:
public class NestedIfElse {
public static void main(String[] args) {
int num = 5;
if (num > 0) {
if (num % 2 == 0) {
System.out.println("Positive Even Number");
} else {
System.out.println("Positive Odd Number");
}
} else {
System.out.println("Non-positive Number");
}
}
}
Output:
Positive Odd Number
Using Logical Operators
Combine multiple conditions using logical operators like &&
(AND) and ||
(OR).
Example:
public class LogicalOperators {
public static void main(String[] args) {
int age = 20;
boolean hasVoterID = true;
if (age >= 18 && hasVoterID) {
System.out.println("You can vote.");
} else {
System.out.println("You cannot vote.");
}
}
}
Output:
You can vote.
Practice Problems
- Write a program to check if a number is positive, negative, or zero using if … else.
- Create a program that categorizes a person as a child, teenager, or adult based on their age.
- Write a program to determine the largest of three numbers using nested if … else.
Tips and Best Practices
- Readable Conditions: Use descriptive variables to make conditions easier to understand.
- Avoid Deep Nesting: Excessive nesting can make your code hard to read. Consider refactoring.
- Default Else: Always include an
else
statement to handle unexpected cases.
Conclusion
The if … else statement is a powerful tool for decision-making in Java. By mastering its usage, you can create dynamic and responsive programs.
For more in-depth tutorials and examples, visit The Coding College and start coding with confidence today! 🚀