When and where we can use Java if…else Condition
When we want to check the condition before executing the code then we can use Java if…else statement to achieve the same.
If condition:
If the conditions inside the brackets is True then statements inside the if block will be executed. The syntax for the if statement is as follows.
Example: Print if the entered number is even. In the following example if the modulo by 2 is 0 then the condition will return as true and the code inside the if block will be printed.
If…else Condition:
In the above example if we also want to print if the integer is odd the then we can use the else block after if and print the statement inside the else block. That is if the condition inside the if is false then statements inside the else block will be executed.
The syntax for the if…else conditions is as follows.
Example: above example for if can be modified as follows to print if the number is odd.
Nested if…else conditions:
We can also have multiple if…else conditions to check multiple possibilities, this is also known as nested if…else conditions. The syntax is as follows.
For example, we can check for 11, 8, 25, 9 to check the output of entered number.