26 August 2012


Java Tutorial #5

  Friends today we start our discussion from Decision making processes & concept of looping , where we
 will see numerous ways by which we can repeat a process or take decision in java & in the end we will
 program few examples in decision making process, looping.
 Decision making statements 
          if statement    
    if satement is mainly used to execute a block of code when a given condition inside the if block is correct.
  We can also add many conditions inside one if loop by applying && or || logical operator discussed in the  
  previous tutorial  







          The basic structure of if block will look somewhat like this:-
               if(test expression)
            {
                 statement block that need to be executed
               }
               statement X;
   The if...else statement 

    if else statement is mainly used to execute both the aspect of a given expression , if a given expression is correct than, block of if part will gets execute & if the given expression doesn't satisfy the above condition than else part of the block will gets executed
                  The proper diagramatic explanation of the if...else statement is shown above.
    The basic structure of if...else block will look somewhat like this:-
                   if(test expression)
             {
                       True block statement
                   }
                   else
                   {
                       False block statement
                   }
                   Statement X;
        
    The else if ladder
 it mainly used to check more than one condition at a time when there is no link between various condition. It
 starts checking the condition start from the first if block & if any of the condition matches it executes that
 block & gets exited from all the remaining blocks if if or else if statement & if none of the if statement
  matches the conditio it simply executes the last else block(if present)
                   The proper diagramatic explanation of the else if ladder is shown above







  The basic structure of else if block will look somewhat like this:-
               if(test expression1)
         {
                   statment-1;
               }
               else if(test expression2)
               {
                   statement-2;
               }
               else if(test expression3)
               {
                   statement-3;
               }
               -----------
               else if(test expressionn)
               {
                   statement-n;
               }
               else
                  default statment; 
                                   
               Statement X;
    
    The switch statement
    It is also somewhat like elseif ladder in which if the case of a given expression matches with the listed case then that case's statement blocks gets executed.
    NOTE:-Friends keep in mind that if we dont use break statement after each case's block ending the block of the case that satisfy the geiven condition & remaining ll the blocks after that case will also gets executed.
  The proper diagramatic explanation of the switch case is shown above



 The basic structure of switch statement will look somewhat like this:-
               switch(expression1)
       {
                         case value-1;
                             block-1
                                break;

                         case value-2;
                             block-2
                                break;
                           -------
                           -------
                          default;                                       
                             default-block
                                break;
                         }          
                  Statement X;
    
   Now lets practice one program that can use all the above explained concepts;

    ___________________________________________________________________
  public class DecisionMaking{
 public static void main(String args[])
 {
  int a=325,b=457,c=478;
  System.out.println("largest value among the following is:");
  if(a>b)
  {
   if(a>c)
   {
    System.out.println(a);
   }
   else
    System.out.println(c);
  }
  else{
   if(c>b)
    System.out.println(c);
   else System.out.println(b);
  }
 }
 }
_________________________________________________________________________________

 Looping in Java
    for loop


 This is one of the most used type of looping that mainly used if we know the number of times you need to
 execute the loop
 The basic structure of the for loop is given below:-

          for(initilization; test condition; increment)
                {
                  body of the loop;
                }
                Statment X;        
    The while statement
    this is also among the famous looping construct that is being mainly used if we dont know the end of the loop or number of times the loop is going to execute
                  The comparison between for & while loop is shown in below drawn figure;-
    The basic structure of the while loop is given below:-
              IntiAlization:
              for(test condition)
                    {
                      body of the loop;
                    }
                    Statment X;        
    The do-while statement


           Till now all the looping conditions we learn use to check condition before executing its block, but
  do-while statement allows us to execute its block once before applying the conditions.
           It is almost same as use once for seeing genuinity of the product before paying to the shopkeeper
  The basic structure of the do-while loop is given below:-

          initilization;
                do
                {
                  body of the loop;
                }
                while(test condition);
                Statment X;
        
 Now lets see an example on looping
  Here we are finding the Computing power of 2;
    _________________________________________________________________________
    class Power{
     public static void main(String args[])
        {
         long p;
            int n;
            double q;
            p=1;
            for(n=0;n<10;++n)  
            {
             if (n==0)
                 p=1;
                 else
                  p*=2;
                 q=1.0/(double)p;
                 Syatem.out.println(" "+q+" "+" "+p);
                  
            }
           }
          }
_____________________________________________________________________________ 

2 comments: