If- Else (conditional statement)

If and else are known as conditional statements.

Conditional statements  are those statements which allows  flow of execution of the program based on certain condition to be true or false.

These are the basic conditional statements:

  1. if statement
  2. if-else statement
  3. nested if-else statement


If statement:

Syntax:#ifelse #if-else #nestedifelse

Boolean expression means condition. To execute the if code boolean_expression must be true.


Syntax:#JavaConditional #ifelse #nestedifelse

If boolean_expression for if statement i.e. for the above condition is false – else code will be executed.

If-else statement:

Syntax:#JavaConditional #ifelse #nestedifelse

Above syntax meaning: If true else false.

That means if the condition(boolean_expression) is true execute and run code statement in if {} or if condition is false execute and run code under else {}.

Let’s see an example:

A person whose age is greater than or equal to 18 is considered an adult. If age less than 18, person is in his/her teenage. You need to write a code condition.java to determine whether a person is an adult or a teenage.]

Try this code.

#ifelse #nestedifelse #ifelseexampleHere you have only one condition. So, you can use simple if-else code syntax. If true (adult) else false(teenage). Below you can see the flow of the above code.

Output = You are a teenager.

#ifelse #nestedifelse #ifelseexample #codeeasyifelse

If-else-if statement:

If there is more than one condition to be followed you cannot use if-else. So, you need to use multiple if-else statement.


#ifelse #nestedifelse #ifelseexample #codeeasyifelse


Let’s see an example:

In a class test grade distribution is as follows:

Grade – A = score equal to or above 85.

Grade – B = score between 60 and 84.

Grade – C = score between 40 and 59.

Fail = score less than 40.

Try this code.

Now here you have 3 conditions. So, you can make use of multiple if – else – if code syntax to execute different code for different condition (as shown in picture).

#ifelseif #multipleelseif

If neither of the 3 conditions is true finally else code is executed.

Output = Grade – B

Below you can see the flow of the above code.

#ifelse #nestedifelse #ifelseexample #codeeasyifelse

Nested if-else Loop:

A nested loop is a loop within a loop. It has an inner loop(if-else) which is in the body of outer loop(if-else).

Syntax:#ifelse #nestedifelse #ifelseexample #codeeasyifelse


From above syntax you can see there are two if statements.

The outer if – loop has an inner if – loop. The inner loops runs only when outer loop condition is true. That is when condition 1 is true then only it jumps to the inner loop.

Lets see an example:

You have been given a task to determine a given number whether it is even or odd. But the number should be greater than 50.

Try this code.

Here you have 2 condition i.e number should be greater than 50 and to find if it is even or odd. Here, condition2 is dependent on the condition1. Condition2 will execute only when condition 1 is true. Condition1 is in outer loop while condition2 is in inner loop.

#nestedifelse #ifelse #nestedifelse

num%2 means num is divided by 2 to give remainder. If this remainder is equal to 0 i.e num%2==0 than num is even otherwise its odd.

Output = Number is greater than 50 and it is even.

Below you can see the flow of the above code.

#ifelse #nestedifelse #ifelseexample

In the further tutorials you will learn how to use these conditional statements with loops.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s