else

Using if structure we can test one condition and execute statements inside if.
But sometime if condition of if structure is false then other set of statements has to be execute.
OR you want to execute a statement if a certain condition is met, and a different statements if the condition is not met.
This is done by using else structure.
else extends an if statement, if condition of if is false.

Syntax of else

Following is a general syntax of else:

 

Flowchart of else

flowchart-of-else
 

Example of if-else –

 

output:

The else statement is only executed if the if expression evaluated to FALSE.
Firstly the if condition is checked, if it is false then else body is execute.

Ankit Wasankar

Senior Java Developer
Java 11 | Microservices | Reactjs | SQL

Help others by sharing the content!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.