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

Asset Color Catalog | Swift Tutorial Asset Color Catalog is the feature of XCode 9. BY the use of this feature we can maintain the colors easily and avoid the mistakes. We can create the ...
Create Gradient layer by the use of CAGradientLaye... Create Gradient layer by the use of CAGradientLayer Mostly developers are use the gradient (Combination of colors) colors images for make app more ef...
Java Operators Java Operators An operator is used to perform an operation over one or more operands. It is a character that represents an action, for example * is a...
Variables in Java Variables in Java A variable is something which is associated with a value can be changed. For example: int i=10; Here variable name “i” wh...
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...
Control Flow Statements | Swift tutorial Control Flow : Swift tutorial Swift Provides diffrent type of controll flow statements like: Loops Statements : Loops are used for perform the t...

Leave a Reply

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