Pages

Thursday, September 8, 2016

if , else if and else statement in java

If statement




if statement is a condition , Like In java if you want to execute something for a specific situation, So if statement is use let's consider the traffic light.



 


Syntax for java if statement:


/* 1. name of if is just = if */
     /*2. the condition for the
     the statement is
     in the round braces
   
      */
      
      
       if(condition){
/*if the condition is true
 * executes body codes
 */
               body

       }



Let's have a brief look at this image below , In this image it shows that

if the light is green = you can go



 


Now the point is how can we apply this condition in java.





    public static void main(String[]args){
   
/*declare light variable*/
      
    String light;
    light = "green";
   
/*if it's gren */
   
    if(light == "green"){
  
    /* if condition true
     * body executes 
     */
      
     System.out.println("you can go");  
    }
}
    



Out put

you can go




Explanation of above example
1. In if statement java compiler first check what is in the round braces, In the above statement we put light =="green" ,

2. So compiler checks here if the light is actually green so what ever in the body part is ,,, it executes.other wise it does nothing.

3. So as we can see the value that we assigned to the light is "green" , So here the if condition is true,Thats why the body executed ,And as a result we have if statement body result which is

you can go.



if and else

In if else statement we have two choices , if and else

ie , If the condition is true so (go ahead) , other wise you will you have another choice ,So this kinds of situation is called if and else statements.



Synatax for if & else 

  if(condition){
  
       body 1
       
   }

else {
      
        body 2
       }



Have a look at the image below






This image says if the light is red , you should stop you car , Other wise you can go,
Let's implements above image with java if statement



public static void main(String[]args){
   
/*declare light variable*/
      
    String light;
    light = "green";
   
/*if it's red */
   
   if(light== "red" ){
      
   System.out.println("Stop");
   }
  
  
  
  /*if it's not red*/
 else{

System.out.println("Go");  
   }

}



Out put

Go



Explanation of the above example

1. Compiler checks , If condition first if the code in the round braces is true. It executes if body, Other wise it goes to else body, In the above example our if condition is false , Because the value of light is not red , Its green so it goes to else and executes else body.






else if statement

If else statement is use when you have multiple condition ( more than one condition ), This statement is a very wide useful statement in java, Because you can handle more than one condition with this statement.Some other charachteristics are as follow. .

Characteristics of else if statement

1. else if statement is start with if(condition) statement .

2.In else if statement , a user may have unlimited , else if(condition) statements , depends on users how much they add.

3. In else if statements you can iclude an else statement as well, This is optional (depends on users, if they add they can.) .


The syntax for this statement is as follow



if(condition){
       body 1
    }
    else if(2nd condition){
       body 2
    }
    else if ( 3rd condition){
       body 3
      
    }
    else if( 4th condition ){
       body
    }
   
    else{
      
    }

    



Example : Let's have a look at the image






In the above example we have three condition.

1. If the light is green . It says Go.

2. If the light is yellow .It says ready for going

3. If the light is red . It says stop.



Now lets implement this image in java actuall coding.



package ifStatement;


public class main {

    public static void main(String[]args){
   
/*declare light variable*/
      
    String light;
    light = "green";
   
/*if it's green */
   
   if(light == "green" ){
      
   System.out.println("Go");
   }
  
  
   /*if the light variable is  yellow*/

 else if(light == "yellow"){
     
System.out.println("Stop");    

}

   /*if the light variable is  red*/
 

 else if(light == "red"){

System.out.println("ready for going!");   
  
   }
 
   /*if none of these*/
  
 else{
     
     System.out.println("none of them!");     

 }
  
  

    }



Out put

Go



Explanation of this example

1. Compiler first checks in the if condition if its true it executes the body of the if .

2. After checking first it then goes to the next condition which else if and do the same.

3. Compiler checks all the statement step by step and reach the last statement which is else..Else statement is not essential.

No comments:

Post a Comment