Fallowing icon

Tuesday, September 25, 2012

Java if else statement



      if-then,if-then-else statements is come with under the decision making statements in Java. This statement is used for control another statement or statements in if-else statement body.


The if-then statement

      if statement is the most basic control statement. it's implementation as fallows. 


       if(Expression){
            statement1;
            statement2;
            ..........;
            ..........;
          }

      If expression true then statements are executed. When the expression false program jump next step without executed any statement under the if condition. if statements are could use quantity of needs on a java code.

Example-


         public class Test { 
    
                    public static void main(String args[]){
        
                               int x=10,y=5;
        
                               if(x==y){
                               System.out.println("X is equal Y");            //When x=y, This statement is executed
                                }
                               if(x>y){
                              System.out.println("X is grater than Y");      //When x>y, This statement is executed
                               }
                              if(x<y){
                              System.out.println("X is less than Y");        //When x<y, This statement is executed     
                              }       
                  }    
         }





//Output is Xis grater then Y


* download this java code, then compile it and run.

Discussion- 

10 and 5 assign to x and y above this example.


        All the if statements are executed one by one.First execute (x==y) statement, But it is false. Secondly execute (x>y) statement, expression is true.Now execute the statement in this condition. finally execute (x<y) statement, But is is false. 




if-then-else statement 


     This statement is had to condition statements. if and another one is else. 
Important- 

* else statement is couldn't use without if statement and could use only one else statement.

It's implementation is fallows-


         if(Expression){
            statement1;
            statement2;
            ..........;
            ..........;
          }
          else {  
            statement1;
            statement2;
            ..........;
            ..........;
          }  

      If expression is true then statements are executed. otherwise else statement is executed. else is optional. 


Example-


    public class Test { 
    
            public static void main(String args[]){
        
                         String myName="Boston";
        
                          if(myName=="Jack"){
                                    System.out.print("My name is Jack");        //declare the if statement    
        
                          }
                          else
                                    System.out.print("My name is not Jack");    //declare the else statement              
          }    
 }





//Output is My name is not Jack

* download this java code, then compile it and run.


Discussion- 

      First execute if condition. But it's expression is false, so execute optional condition. 

Important-
      When the if condition's expression is true, program isn't executed optional condition. 



if-then-else if-then-else statement 


       This statement have tree main conditions, first one is if second one is else if other one is else.

Important-

* else if statements are could use quantity of needs on a java code but else statement is could use only one a java code. 

  
 It's implementation is fallows-
       
       

        
 if(Expression){
            statement1;
            statement2;
            ..........;
            ..........;
          }
         else if(Expression){
            statement1;
            statement2;
            ..........;
            ..........;
          }
          else {  
            statement1;
            statement2;
            ..........;
            ..........;
          } 


   
        if condition's expression is true then statements are executed. But expression is false, Now check else if condition's expression. It's true, executed those statements otherwise else statement is executed. else is optional.


Example-



      public class Test { 
    
                  public static void main(String args[]){
        
                              int value=10;                                                         //declare the value variable and assign 10
       
                              if(value ==9){                                                        //declare the if condition
                             System.out.println("Value is 9");
                             }
       
                            else if(value==10)                                               //declare the else if condition
                            System.out.println("Value is 10");
       
                           else                                                                      //declare the else condition
                           System.out.println("Value is not 9 or 10");       
                   }    
        }




//Output is Value is 10

* download this java code, then compile it and run.

Discussion-

  First we create variable and assign 10 for it.

   Now check if condition, But it's expression is false, because value!=9. secondly check else if condition, It's expression is true. So execute else if  statement. else condition is't executed in this example, because else if condition is true.
          
       
    

No comments:

:a   :b   :c   :d   :e   :f   :g   :h   :i   :j   :k   :l   :m   :n   :o   :p   :q   :r   :s   :t

Post a Comment