What is if else statement?

The if-else statement used to executes a certain block of code based on the state of variables. In simple words whenever we need to execute a set of statements based on some condition then we use control flow statements.

See the flow diagram that checks if the user is eligible to vote for its leader or not –

If else flow

From the above flow-diagram, we can clearly see, if the user’s age is greater than or equal to 18 than he/she is eligible to vote otherwise not. Likewise, we can use if-else statements if different ways to control the flow of the program.

Types of if-else

There are four different types of if-else available in Java –

  1. Simple if statement
  2. If-else statement
  3. Ladder if-else statement
  4. Nested if-else statement