Java If-else
If-Else statements are a fundamental part of programming in Java. They are used to make decisions based on a given condition. If the condition is true, a certain set of instructions will be executed; if the condition is false, a different set of instructions will be executed.
Example
int number = 15;
if (number > 10) {
System.out.println(Number is greater than 10);
} else {
System.out.println(Number is not greater than 10);
}
Suppose we want to check if a number is greater than 10. We can use an if-else statement to do this
In this example, the condition is number > 10. If the condition is true (in this case, it is true because 15 is greater than 10), the statement inside the if block will be executed.
If the condition is false (in this case, it is false because 15 is not less than 10), the statement inside the else block will be executed.
In this example, the output will be a Number is greater than 10.
If-else statements are a powerful tool for making decisions in Java. They can be used to check for a variety of conditions and execute different sets of instructions based on the result.