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

Custom View by use of xib in swift Custom View by use of xib in swift We can create the custom and common view by the use xib in swift 3.1 and xcode 8.3.2 . Custom views are common and...
How to install flutter | Flutter tutorial Flutter is the google’s mobile app SDK for develop the high-quality native app for iOS and android. We can develop the app by use of DART and javascri...
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...
Decision Control Statement C has three major decision making instructions— the if statement, the if-else statement, and the switch statement. The if Statement It is used to ex...
Variables Variables A variable is a named location in memory that is used to hold a value that can be modified by the program. All variable must be declared be...
Continue Statement in JAVA Continue Statement in JAVA Suppose you are working with loops. Sometime it is desirable terminate the loop or skip some statement inside the loop wit...

Leave a Reply

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