Skip to main content

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.

Comments

Popular posts from this blog

What is Java

Java is a general purpose programming language. Which means this programming language is used in many purposes apart from being used only in android application development. There are a few similarities between Java programming language and C++ programming language. But Java programming language is having some advanced features which you are going to learn in future. In programming languages you write programs in the form of statements in many lines and those statements are executed one by one. In Java, the execution happens really fast and concurrently it can execute many statements. Java is a platform independent (Java compiled byte code can be run across different operating systems) and object oriented programming language.   Why should we learn Java programming language?   Easy to learn: Java is a easy to learn programming language. Once you start learning Java programming, you will actually be learning the real programming and you will observe everyth...

Java User Input Exercise

Exercise 1: Write a simple java program to take user input of a number and store it inside an integer variable. Later print the number on the screen.   Solution 1:   import java.util.Scanner;   public class UserInpur {        public static void main(String[] args ) {               Scanner sc = new Scanner(System. in );           System. out .println( "Enter a number" );           int x = sc .nextInt();           System. out .println( "You have entered:" );                   System. out .println( x );        }   }   Exercise 2: Write a simple java program to tak...

Java Operators

In this lesson you are goin g to learn about operators in Java and also what are the different types of operators used in Java programming.   In our previous lesson we have learnt how to take user input in Java. We were performing different types of arithmetic operations in order to create a simple ration shop billing application which took different values as the user input and calculated total billing amount.   During the programming the billing application we used different signs like addition (+) , multiplication (*) etc. These signs are called operators in Java programming.   Types of Operators in Java There are five types of operators in Java. Arithmetic Operators Bitwise Operators Assignment Operators Comparison Operators Logical Operators 1.Java Arithmetic Operators Arithmetic operators are used to perform mathematical operations such addition, division, multiplication, subtraction and modulation. Let’s see the example of arithmetic operators in below:   ...