3.1. If Statements
If statements are fundamental to decision-making in Java. They allow the program to execute specific blocks of code based on whether a condition evaluates to true or false. This enables dynamic behavior and logical branching in your programs.
Syntax of If Statements
The basic syntax of an if statement is:
if (condition) {
// Code to execute if condition is true
}Example:
int number = 10;
if (number > 5) {
System.out.println("The number is greater than 5.");
}In this example, the message is printed because the condition number > 5 evaluates to true.
If-Else Statements
The if-else statement provides an alternative block of code to execute when the condition is false.
Syntax:
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}Example:
int number = 3;
if (number % 2 == 0) {
System.out.println("The number is even.");
} else {
System.out.println("The number is odd.");
}Else-If Ladder
The else-if ladder is used to test multiple conditions sequentially. The first condition that evaluates to true will execute its corresponding block of code.
Syntax:
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else {
// Code to execute if none of the conditions are true
}Example:
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 {
System.out.println("Grade: F");
}Nested If Statements
You can nest if statements inside one another to handle more complex conditions.
Example:
int age = 20;
boolean hasLicense = true;
if (age >= 18) {
if (hasLicense) {
System.out.println("You can drive.");
} else {
System.out.println("You need a license to drive.");
}
} else {
System.out.println("You are too young to drive.");
}Logical Operators in Conditions
Logical operators are often used in if statements to combine multiple conditions:
| Operator | Description | Example | | -------- | --------------------------- | ----------------- | ------------------------ | ------ | --- | ------- | | && | Logical AND (both true) | a > 5 && b < 10 | | | | | Logical OR (either true) | a > 5 | | b < 10 | | ! | Logical NOT (negates truth) | !(a > 5) |
Example:
int age = 25;
boolean hasID = true;
if (age >= 18 && hasID) {
System.out.println("Access granted.");
} else {
System.out.println("Access denied.");
}Common Pitfalls and Best Practices
Missing Braces: Always use braces
{}for clarity, even for single-line blocks.javaif (x > 0) System.out.println("Positive"); // Avoid thisOverlapping Conditions: Ensure conditions in
else-ifladders are mutually exclusive.Complex Conditions: Break down complex conditions into smaller, readable parts.
Testing Edge Cases: Always test boundary values for conditions.
Example Code Snippet
public class IfStatementExamples {
public static void main(String[] args) {
int temperature = 30;
if (temperature > 35) {
System.out.println("It's very hot.");
} else if (temperature > 25) {
System.out.println("It's warm.");
} else {
System.out.println("It's cold.");
}
int number = 15;
if (number % 3 == 0 && number % 5 == 0) {
System.out.println("FizzBuzz");
} else if (number % 3 == 0) {
System.out.println("Fizz");
} else if (number % 5 == 0) {
System.out.println("Buzz");
} else {
System.out.println(number);
}
}
}