Java If-else Statement

Java If-else Statement

The Java if statement is used to test the condition. It checks boolean condition: true or false.

For example, if a number is greater than zero then we want to print “Positive Number” but if it is less than zero then we want to print “Negative Number”. In this case we have two print statements in the program, but only one print statement executes at a time based on the input value.

There are various types of if statement in java.
a) if statement
b) nested if statement
c) if-else statement
d) if-else-if statement

a) if statement

If statement consists a condition, followed by statement or a set of statements as shown below:

Syntax:

1
2
3
<span class="hljs-keyword">if</span>(condition){  
<span class="hljs-comment">//code to be executed  </span>
}

The statements gets executed only when the given condition is true. If the condition is false then the statements inside if statement body will not be executed.

Example:

1
2
3
4
5
6
7
8
9
10
11
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">IfStatementExample</span> </span>{
   <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String args[])</span></span>{
      <span class="hljs-keyword">int</span> num=<span class="hljs-number">250</span>;
      <span class="hljs-keyword">if</span>( num &lt; <span class="hljs-number">300</span> ){
      <span class="hljs-comment">/* This println statement will only execute,
       * if the above condition is true
       */</span>
      System.out.println(<span class="hljs-string">"number is less than 300"</span>);
      }
   }
}
Output:

number is less than 300

b) nested if statement

When there is an if statement inside another if statement then it is called the nested if statement.

Syntax

1
2
3
4
5
6
7
<span class="hljs-keyword">if</span>(condition_1) {
   Statement1(s);

   <span class="hljs-keyword">if</span>(condition_2) {
      Statement2(s);
   }
}
Example:

1
2
3
4
5
6
7
8
9
10
11
12
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NestedIfExample</span> </span>{

   <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String args[])</span></span>{
        <span class="hljs-keyword">int</span> num=<span class="hljs-number">70</span>;
    <span class="hljs-keyword">if</span>( num &lt; <span class="hljs-number">100</span> ){
           System.out.println(<span class="hljs-string">"number is less than 100"</span>);
           <span class="hljs-keyword">if</span>(num &gt; <span class="hljs-number">50</span>){
          System.out.println(<span class="hljs-string">"number is greater than 50"</span>);
       }
    }
   }
}
Output:

number is less than 100
number is greater than 50

c) If else statement

The Java if-else statement also tests the condition. It executes the if block if condition is true otherwise else block is executed.

Syntax:

1
2
3
4
5
6
<span class="hljs-keyword">if</span>(condition) {
   Statement(s);
}
<span class="hljs-keyword">else</span> {
   Statement(s);
}

The statements inside “if” would execute if the condition is true, and the statements inside “else” would execute if the condition is false.

Example:

1
2
3
4
5
6
7
8
9
10
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">IfElseExample</span> </span>{  
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{  
    <span class="hljs-keyword">int</span> number=<span class="hljs-number">13</span>;  
    <span class="hljs-keyword">if</span>(number%<span class="hljs-number">2</span>==<span class="hljs-number">0</span>){  
        System.out.println(<span class="hljs-string">"even number"</span>);  
    }<span class="hljs-keyword">else</span>{  
        System.out.println(<span class="hljs-string">"odd number"</span>);  
    }  
  }  
}
Output:

odd number

d) if-else-if Statement

The if-else-if statement executes one condition from multiple statements. In this statement we have only one “if” and one “else”, however we can have multiple “else if”. It is also known as if else if ladder.

Syntax:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<span class="hljs-keyword">if</span>(condition_1) {
   <span class="hljs-comment">/*if condition_1 is true execute this*/</span>
   statement(s);
}
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(condition_2) {
   <span class="hljs-comment">/* execute this if condition_1 is not met and
    * condition_2 is met
    */</span>
   statement(s);
}
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(condition_3) {
   <span class="hljs-comment">/* execute this if condition_1 &amp; condition_2 are
    * not met and condition_3 is met
    */</span>
   statement(s);
}
...
<span class="hljs-keyword">else</span> {
   <span class="hljs-comment">/* if none of the condition is true
    * then these statements gets executed
    */</span>
   statement(s);
}
NOTE: In if-else-if statement, as soon as the condition is met, the corresponding set of statements get executed, rest gets ignored. If none of the condition is met then the statements inside “else” gets executed.
Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Sample</span> </span>{

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String args[])</span> </span>{
        <span class="hljs-keyword">int</span> a = <span class="hljs-number">30</span>, b = <span class="hljs-number">30</span>;

        <span class="hljs-keyword">if</span> (b &gt; a) {
            System.out.println(<span class="hljs-string">"b is greater"</span>);
        }
        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(a &gt; b){
            System.out.println(<span class="hljs-string">"a is greater"</span>);
        }      
        <span class="hljs-keyword">else</span> {
            System.out.println(<span class="hljs-string">"Both are equal"</span>);
        }
    }
}
Output:

Both are equal

Related Post

Data Types in Java Data Types in Java Data types represent the different values to be stored in the variable, for example if a variable has int data type, it ...
New Tools for 64-bit Support in Xcode 9.3   New Tools for 64-bit Support in Xcode 9.3 The last macOS release to support 32-bit apps without compromise is macOS High Sierra. Make sure fu...
Break Statement in JAVA Break Statement in JAVA The break statement is usually used in following two scenarios: The break statement terminates the loop immediately, and...
Drag and Drop Table View cell in IOS 11 & swi... Drag and Drop Table View cell in IOS 11 & swift 4
Array An array is a collection of similar kind of data elements stored in adjacent memory locations and are referred to by a single array-name. The first el...
First Java Program First Java Program NOTE: You need to have java installed on your system. Step 1: Open a text editor, like Notepad on windows and TextEdit on Mac. W...

Leave a Reply

Your email address will not be published. Required fields are marked *

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.