In Java, the
if-else if statement is used to make decisions based on specific conditions. It helps you write code that can respond to multiple situations, where each situation has a condition that needs to be met.How does the if-else if statement work?
- if: You start with the
ifstatement, which contains the first condition. If this condition is true, the code inside this block runs. - else if: If the first condition is false, you move to the next condition using
else if. You can add as manyelse ifconditions as you need, each time checking a different condition. - else: Finally, if all previous conditions are false, the
elseblock runs as the default option.
Example:
Here's an example to make it clearer:
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: D");
}

Wonderful , Bilal keep going
ReplyDeleteperfect 🤩
ReplyDelete