When and where we can use Java if…else Condition

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.

Java if...else Condition

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.

Java if...else Condition

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.

Java if...else Condition

Example: above example for if can be modified as follows to print if the number is odd.

Java if...else Condition

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.

Java if...else Condition

For example, we can check for 11, 8, 25, 9 to check the output of entered number.

Java if...else Conditions

-A blog by Shwetali Khambe

Related Posts