Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Java - If Else

In our everyday life we have to take one or the other decision.


Example :

To reach our office we have to take the decision that if we get a bus we will ride in it else we will be hiring a cab.

Even java has to make some decisions. And there are ways to write it :

  1. if

  2. else

  3. else if

So, when we write the above 'going to office by bus or cab' scenario in java, it would be :



Decision making using 'if'

public class Test{
  public static void main(String[] arg){
    String vehicle = "Bus";

    if(vehicle.equals("Bus")){
     System.out.println("Will go to office by Bus.");
    }

  }
}


Output :


   Will go to office by Bus.
java_class_object

In the above code we have created a String type variable 'vehicle' and assigned the value "Bus" to it.


String vehicle = "Bus";

Then we are telling java. If it finds that the String type variable 'vehicle' has value 'Bus'. Get into the block of 'if' statement and print 'Will go to office by Bus.':


if(vehicle.equals("Bus")){
   System.out.println("Will go to office by Bus.");
}

Note :.equals() method of String checks, if two values are equal or not.

Now, let us make a small change in code :


public class Test{
  public static void main(String[] arg){
    String vehicle = "Car";

    if(vehicle.equals("Bus")){
      System.out.println("Will go to office by Bus.");
    }

  }
}

Output :


  

In the above code there is no output.

java_class_object

It is because, in this case the variable 'vehicle' is having the value 'Car'.


String vehicle = "Car";

Now, using the 'if' statement, when java checks, if the value of 'vehicle' is 'Bus' or not. It does not find because the value of the 'vehicle' is 'Car'.


if(vehicle.equals("Bus")){
   System.out.println("Will go to office by Bus.");
}

And thus it does not enter the 'if' block and 'Will go to office by Bus.' is not printed.


Decision making using 'if' and 'else'

Example :

It is just like, if you don't get a bus. You go by a cab.


public class Test{
  public static void main(String[] arg){
    String vehicle = "Car";

    if(vehicle.equals("Bus")){
     System.out.println("Will go to office by Bus.");
    }
    else {
     System.out.println("Will take a Cab.");
    }
  }
}

Output :


   Will take a Cab.

Since, the 'if' condition didn't meet :


if(vehicle.equals("Bus")){
   System.out.println("Will go to office by Bus.");
}

It came to the 'else' statement and printed the value 'Will take a Cab.' inside the 'else' statement.


else {
   System.out.println("Will take a Cab.");
}

Decision making using 'if', 'else if' and 'else'

Even in the 'else' statement we can put an 'if' condition. Just to check if the second condition meets or not.


Example :

Say there can be a situation that if we do not get a 'Bus', we will try to go by a cab. And if the cab is also not available. Simple, we won't go to the office.


public class Test{
  public static void main(String[] arg){
    String vehicle = "No Vehicle";

    if(vehicle.equals("Bus")){
     System.out.println("Will go to office by Bus.");
    }
    else if(vehicle.equals("Cab")) {
     System.out.println("Will take a Cab.");
    }
    else {
     System.out.println("Won't go to office.");
    }
  }
}

Output :


   Won't go to office.

In the above code, we have initialized the variable 'vehicle' with 'No Vehicle'(Suppose no vehicle is available).

java_class_object

The first condition doesn't meet, as we are checking 'vehicle' variable is holding the value 'Bus' or not.


if(vehicle.equals("Bus")){
   System.out.println("Will go to office by Bus.");
}

Then it comes to the else part, where there is an if statement again, checking if 'vehicle' variable is holding 'Cab' or not.


else if(vehicle.equals("Cab")) {
   System.out.println("Will take a Cab.");
}

It doesn't match, and it comes to the final else and prints the contents inside the 'else' block.


else {
   System.out.println("Won't go to office.");
}