If…Else Statement in Java

If-else statements are used as conditional programming in java language. This means the program will only run if a defined condition returns true value.

In java operator’s lesson, you have learned about Java Comparison Operators. These comparison operators play a very crucial role in Java if else statement.

There are mainly 3 types of If else statements in Java programming.

  • If statements
  • Else statements
  • Else if statements

 

If statement in Java:

Java if statement is a set of codes which is only executed if a condition returns true value.

 

Syntax of Java If statement:

 

if (Condition) {

 

This statement inside the curly braces will only get executed if condition returns true value.

 

}

 

In the syntax you can see the condition part is written inside the parenthesis and if this condition is true then only the statement which is written inside the curly braces will be executed.

 

Programming example of Java if statement:

 

package if_else_java;

 

public class if_else_intro {

 

    public static void main(String[] args) {

        

         int a=10;

         int b=20;

        

         if (a>b) {

              System.out.println(a+" is greater than "+b);

         }

        

         if (b>a) {

              System.out.println(b+" is greater than "+a);

         }

        

    

    }

 

}

 

 

 

Output in the Console:

20 is greater than 10

 

 

In the above example you can see we have used two variables and assigned them with two different values.

Now, the ‘if statement’ is executed only when the condition is fulfilling.

As you can see, the value of variable-a is not greater than variable-b, so the first condition is not fulfilling.

In the second condition the code is returning a true value and hence the statement inside the second if statement is only getting executed.

 

Important Note: Please note that, java is a case-sensitive language so make sure you write ‘if and else’ in the statement in lowercase only and if you write ‘IF and ELSE’ in capital letters, the compiler will return an error.

 

Else statement in Java:

Java else statement is a set of codes which is only executed when the ‘if condition’ returns a false value.

 

Syntax of Java Else statement:

 

if (Condition) {

 

This statement inside the curly braces will get executed when the ‘if condition’ returns a true value.

 

} else{

 

This statement inside the curly braces will get executed when the ‘if condition’ returns false value.

 

}

 

In the syntax above you can see the ‘else condition’ part is written inside the parenthesis and this ‘else part’ will only get executed when the ‘if part’ returns a false value.

 

Programming example of Java else statement:

 

package if_else_java;

 

public class if_else_statement {

 

     public static void main(String[] args) {

         

          int a=100;

          int b=200;

         

          if (a>b) {

              System.out.println(a+" is greater than "+b);

          }else{

              System.out.println(b+" is greater than "+a);

          }

         

     }

 

}

 

 

 

Output in the Console:

  200 is greater than 100

 

 

In the above example you can see, we have used two variables and assigned them with two different values.

Now, the ‘if statement’ is executed only when the if-part of the condition is fulfilled, otherwise the program will execute the else-part of the code.

Since the value of ‘a variable’ is not greater than the ‘b variable’, the above program is executing the else-part of the code.

 

Else if statement in Java:

Java ‘else if’ statement is a set of codes which is only executed when the first ‘if condition’ returns a false value.

 

Syntax of Java Else if statement:

 

if (Condition) {

 

             // This statement executes when ‘if-condition’ returns true.

 

} else if (Condition){

 

             //This statement executes when ‘else if-condition’ returns true.

 

}else{

 

             //This statement executes when ‘if’ and ‘else-if’ both returns false.

 

}

 

 

In the syntax above, you can see the ‘else-if condition’ is written inside the parenthesis and this ‘else-if statement’ will only get executed when the first ‘if part’ returns a false value and ‘else-if’ condition is true.

 

Programming example of Java else if statement:

 

package if_else_java;

 

public class if_else_statement {

 

     public static void main(String[] args) {

         

          int a=200;

          int b=200;

         

          if (a>b) {

              System.out.println(a+" is greater than "+b);

          }else if(a==b){

              System.out.println("The value of variable a and b are same.");

          }else {

              System.out.println(b+" is greater then "+a);

          }

         

     }

 

}

 

 

 

Output in the Console:

  The value of variable a and b are same.

 

 

In the above example you can see, we have used two variables and assigned them with two different values.

Now, the ‘if statement’ is executed only when the if-part of the condition is fulfilled, otherwise the program will evaluate the else-if part of the code.

If the ‘else-if’ part returns a true value, the else if statement is executive or else the ‘else-part’ statement is executed.

Since the value of ‘a variable’ is just equal to ‘b variable’, the above program is executing the ‘else-if’ part of the code.

 

Nested if-else statement in Java:

Nested if-else statement is a ‘if-else’ statement written inside another ‘if-statement’.

Which means the inner ‘if-else’ statement will only be executed when the outer ‘if’ statement returns a true value.

 

Programming example of Java Nested if-else statement:

 

package if_else_java;

import java.util.Scanner;

 

public class if_else_statement {

 

    public static void main(String[] args) {

 

         Scanner sc = new Scanner(System.in);

        

         int age;

         int percentage;

         int height;

        

         /*

          * Requirement for participation

          * Age not more than 25

          * Percentage not below 50%

          * Minimum height 6 feet

          */

        

         System.out.println("Please enter your age:");

         age=sc.nextInt();

         System.out.println("Please enter your percentage in graduation:");

         percentage=sc.nextInt();

         System.out.println("Please enter your height:");

         height=sc.nextInt();

 

        

         if(age<=25) {

              if(percentage>=50) {

                  if(height>=6) {

                       System.out.println("Congratulation! You are eligible to participate.");

                  }else {

                       System.out.println("Sorry you can not participate. Minimum required height is 6 feet.");

                  }

              }else {

                  System.out.println("Sorry you can not participate. Your percentage is below 50%.");

              }

         }else {

              System.out.println("Sorry you can not participate. Your age is more than 25.");

         }

        

        

    }

 

}

 

 

 

 

Output in the Console:

Please enter your age:

22

Please enter your percentage in graduation:

50

Please enter your height:

6

Congratulation! You are eligible to participate.

 

 

Sum Up:

In this lesson we have discussed about java ‘if-else’ statements. This lesson is very important in java programming because almost in every complex coding ‘if-else’ statement are used. Without learning the ‘if-else’ concept you cannot be a good programmer.

If…Else Statement in Java If…Else Statement in Java Reviewed by Technobits on March 29, 2021 Rating: 5

No comments:

Powered by Blogger.