Latest :

Java If Else – Tutorial With Examples | Learn Java

If else Java – statement complete tutorial. Here we cover in-depth information with examples on what is if else in java and how it works in programming language.

You can learn basics of if else, where to apply statements in programming. Here learn more about control statements in Java and java tutorials for beginners. Also, don’t forget to check out the Java Interview questions with sample programs and examples. The following article was written by path thomas. 

Definition Of If Else Java

If else is a selection statement. It means it can be used to select a block for execution based on a condition. This (if-else) is also known as ” conditional branch statement “. Based on the usage if else in java is classified into different types as follows :

  • Simple If Else
  • Simple If
  • Nested If Else
  • If else Ladder
  • Nested If .. Etc

Java if else is a ” construction “ used to select one out of two possibilities. With nested if-else construction, we can select one out of multiple possibilities. With a simple if construction, we can choose whether a statement (possibility) should be executed or not.

1.Simple If Else

Syntax for the Simple statement with examples as follows :

Example for Simple If else in Java: – 1


In case if the condition is true, then if block is executed and in case the condition is false, then else block is executed. The condition should always result in a Boolean value. Other types of values are not allowed (in C, other types are also allowed).

There will never be a situation where both parts (if and else) will be executed. Only one part will be executed for sure. Skipping both parts is also not possible.

  • if(4<9)                                                   is valid
  • if(4==9)                                                  is valid
  • if(4)                                                         is not valid
  • if( System.out.println(“hello”) )       is not valid
  • if( Math.sqrt(90) < 10 )                      is valid
  • if(Math.pow(2,5) < 10 )                      is valid

we can write any number of statements in an if/else block. Those statements should be enclosed within { and }. In case we have only one statement, then the curly braces are optional.


2.Simple If

  • Inside “if”, there should be at least one statement and in “else” also there should be at least one statement.

The following is not valid because there is no statement in “if” part.

  • If we don’t want to perform any action in case, the condition is true, then we should use a semi-colon (a dummy statement) as a place holder. So the following is valid:

  • similarly if we want to perform some action when the condition is true, and don’t want any action when the condition is false then we can write like the following:

In fact, the “else” part is optional in the if-else construction. So the above code can be written as follows also:

  • This kind of construction is known as simple if construction.

Multiple Conditions Together:

Sometimes we need to take decisions based on multiple conditions. In that case those conditions can be combined with logical operators like the following:

Simple If Example Program:

3.Nested Java If Else Statement:

  • Sometimes we need to check “one condition” only when some other condition is satisfied. In that case we can have an java if else construction inside another if/else part(s) like the following.
From, the above example,

  • partA is executed when condition1 is true and condition2 is true.
  • partB is executed when condition1 is true and condition2 is false.
  • partC is executed when condition1 is false and condition3 is true.
  • partD is executed when condition1 is false and condition3 is false.

This kind of if-else construction is known as ” nested if-else construction “. There is no limit on the depth of nesting. With this usage, we can select one option out of multiple possibilities.

Example Program:


4.Nested If In Java

Sometimes we write “nested if-else” constructions without else parts. Such constructions are generally known as nested-if constructions.

5.If-else ladder

Sometimes we write nested if-else constructions where the nesting takes place in the else parts only. These kinds of constructions are known as if-else ladder (or else-if ladder or if-else-if ladder).



6. Series of if-else

We can have any number of if-else constructions in a program. If they are not nested, one if-else has no relation with the other one.

Example Program:


There should not be any statements between if part and its corresponding else part. So the following code is not valid.

here, the statement b=20; will break the association between if and else parts.

Condition should always be placed at if part only. We cannot use a condition at else part. So the following code is not valid.


Check Also

What is Recursion In Java Programming – JavaTutoring

What is Recursion In Java programming – Here we cover in-depth article to know more ...