Welcome back, Java enthusiasts! In this article, we will explore the if condition, one of the fundamental control flow statements in Java. The if condition allows your program to make decisions and execute different code blocks based on certain conditions. Let’s dive in and see how it works, along with practical examples to illustrate its usage.
Basic If Condition
The basic if statement evaluates a boolean expression and executes a block of code if the expression is true.
Syntax:
if (condition) {
// Code to be executed if condition is true
}
Example:
public class IfConditionExample {
public static void main(String[] args) {
int age = 18;
if (age >= 18) {
System.out.println("You are an adult.");
}
}
}
In this example, the message “You are an adult.” will be printed because the condition age >= 18
evaluates to true.
If-Else Condition
The if-else statement provides an alternative block of code to execute if the condition is false.
Syntax:
if (condition) {
// Code to be executed if condition is true
} else {
// Code to be executed if condition is false
}
Example:
public class IfElseConditionExample {
public static void main(String[] args) {
int age = 16;
if (age >= 18) {
System.out.println("You are an adult.");
} else {
System.out.println("You are a minor.");
}
}
}
In this example, the message “You are a minor.” will be printed because the condition age >= 18
evaluates to false.
If-Else If-Else Ladder
The if-else if-else ladder is used to check multiple conditions. Once a condition is true, the corresponding block of code is executed, and the rest of the ladder is skipped.
Syntax:
if (condition1) {
// Code to be executed if condition1 is true
} else if (condition2) {
// Code to be executed if condition2 is true
} else {
// Code to be executed if all conditions are false
}
Example:
public class IfElseIfElseLadderExample {
public static void main(String[] args) {
int marks = 85;
if (marks >= 90) {
System.out.println("Grade: A+");
} else if (marks >= 80) {
System.out.println("Grade: A");
} else if (marks >= 70) {
System.out.println("Grade: B");
} else if (marks >= 60) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: F");
}
}
}
In this example, the message “Grade: A” will be printed because the condition marks >= 80
evaluates to true.
Nested If Statements
Nested if statements are if statements inside another if or else block. They are used when you need to check multiple conditions that depend on each other.
Syntax:
if (condition1) {
// Code to be executed if condition1 is true
if (condition2) {
// Code to be executed if condition1 and condition2 are true
}
}
Example:
public class NestedIfExample {
public static void main(String[] args) {
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.");
}
}
}
In this example, the message “You can drive.” will be printed because both conditions age >= 18
and hasLicense
are true.
Logical Operators in If Conditions
Logical operators can be used in if conditions to combine multiple boolean expressions. The most common logical operators are:
&&
(AND): Both conditions must be true||
(OR): At least one condition must be true!
(NOT): Inverts the boolean value
Example:
public class LogicalOperatorsExample {
public static void main(String[] args) {
int age = 20;
boolean hasLicense = false;
if (age >= 18 && hasLicense) {
System.out.println("You can drive.");
} else {
System.out.println("You cannot drive.");
}
}
}
In this example, the message “You cannot drive.” will be printed because the condition hasLicense
is false, making the combined condition age >= 18 && hasLicense
false.
Conclusion
The if condition is a powerful control flow statement in Java that allows your program to make decisions based on certain conditions. We covered the basic if condition, if-else condition, if-else if-else ladder, nested if statements, and using logical operators in if conditions. Practice using these concepts to create more dynamic and responsive Java programs.
Next up: We’ll explore the switch statement in Java, another control flow statement that provides an alternative to using multiple if-else-if conditions. Keep coding and happy learning!