In Java, if-else is a control flow statement used to execute certain blocks of code based on whether a condition is true or false.
Syntax:
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
Example:
int number = 10;
if (number > 0) {
System.out.println("The number is positive.");
} else {
System.out.println("The number is not positive.");
}
How it works:
- The condition inside the
ifstatement is evaluated. - If it's
true, the code block inside theifis executed. - If it's
false, the code block inside theelseis executed.
Let me know if you want to go over else-if or nested if too!
Top comments (0)