Introduction to JAVA Programming: Types of if - else statements

in #java6 years ago (edited)

In JAVA, when we program and we want to test a set of condition, the if - else statement comes into play. An if - else statement is used to test conditions for a program. It can also be used for error - trapping.

  • if statement

The if statement can stand on its own and else statement is optional. For if statement it will be executed only if the condition is met or is true. If it is false, then nothing happens.

If.PNG

  • nested if statement
    The nested if statement is a structure where there is a condition within another condition. If the first condition is met then it will proceed and if the 2nd condition fails, then nothing happens. The statement will only run if both conditions are true.

ifelse_3.3.PNG

  • if-else statement
    The if statement will execute if the condition is met or is true and the else statement will execute if the condition fails.

else_3.PNG

  • if - else - if statement
    This statement is used when a program needs to check for multiple conditions.
    Note that there can be only one if and else statement, but else if statement can have multiple occurrence.

ifelse_3.4.PNG

I Hope this helps. Thanks for taking the time to read :)

Sort:  

You have a minor misspelling in the following sentence:

Note that there can be only one if and else statement, but else if statement can have multiple occurence.
It should be occurrence instead of occurence.