Saturday 29 October 2016

Java Language - Decision making branching and looping

Decision making branching and looping in Java:

As we know that the instructions, which we are written in Java language, are executed in sequential order when we compile the program. If we want to execute certain instructions based on the condition, then we apply either branching or looping in the program.

Decision making - branching: we will follow the three Java statements in this regard. The three statements are
i) if statement   ii) switch statement iii) the ?: operator

i) The if statement takes three formats. a) simple if statement b) if - else statement c) nested if statement d) else-if ladder

a) Simple if statement

Systax:
                    if(condition)
                    {
                         statement-1;
                         statement-2;
                             ....
                         statement-n;
                     }
                    statement-k;



Control checks the condition first.  If condition is true, then control enters into if statement and statements which are kept in brace brackets executed once and after that, controls comes out of the if statement.
Ex.
if (n%2==0)
{
                                System.out.println(“ Number is even”);
}

If n value is 20, then condition becomes true then the output statement is executed and message “Number is even”  will be displayed.

b) Syntax: if – else statement
if (condition)
{
                                    Statement-1;
Statement -2;
Statement-n;
}
else
{
Statement-a;
Statement -b;
Statement-z;
}

Execution process:

If condition is true, the control enters into the if statement block and statement-1, statement-2, .. Statement- n are executed and else part is vomited from the execution process. That means  statement-a, statement-b … statement-z are not included in the execution.  On the other hand, if condition is false, else part statements are executed and the block mentioned before the else are not participated in the execution process.  

Ex:
                                if(n%2==0)
   {
      System.out.println(“number is even”);
   }
         else
                {
                    System.out.println(“number is odd”);
                }


If n value 20, the message “number is even” displayed. If n value is 21 the message “number is odd” is displayed.




c) Systax: nested if

if(condition-1)
         {
                                if(condition-2)
                                 {
                                    Statement-1;
                                    Statement-2;
                                         
                                    Statement-3;
                                }
                        }


Nested if statements are allowed in Java programming.  That means, we have to able to use one if statement within the other if statement. Depending upon the requirements of the program we are able to use any number of if statements within the other if statement.

Ex:

class A
{
   public static void main(String args[])
{
      int x = 30;
      int y = 3;
      if( x == 30 )
            {
                 if( y == 3 )
                      {
                          System.out.println("Number 30 is completely divisible by 3);
                     }
           }
   }
  }

The condition x==30 is true here, then control enters into the first if statement block and checks condition for true or false value, if it is true (in present case It is true), the block of statements are executed. Here in this particular example, the message “Number 30 is completely divisible by 3” is displayed as two conditions are true.






d) Syntax: else if else ladder

if(condition-1)
{
     Statement-1;
     Statement-2;                when condition-1 true only these statement are executed
     
     Statement-n;
}
 else if (condition-2)
 {
      Statement-1;
                  Statement-2;                When condition-2 true only these statements are executed
                   ….
                  Statement-n;
  }
. . .
else if (condition-n)
{
      Statement-1;
                  Statement-2;                 When condition-n true only these statements are executed
                   ….
                  Statement-n;
}
else
{
   Statement-1;
               Statement-2;                    All above conditions get false, then these statements are executed
                   ….
               Statement-n;
              }


Ex: 

class ladder
{
     public static void main(String args[])
     {
         
         int demo=3;

         if(demo==1)
         {
             //This block will be executed only if "demo" is equal to 1
             System.out.println("Hyderabad");
         }
         else if(demo==2)
         {
             //This block will be executed only if "demo" is equal to 2
             System.out.println("Warangal");

         }
         else if(demo==3)
         {
             //This block will be executed only if "demo" is equal to 3
             System.out.println("Nalgonda");
         }
         else
         {
             System.out.println("No Place Found");
         }
     }
}


The output for this program is Nalgonda  


ii) Switch Statement

Sytax:


switch(expression) {
 
   case label1  :
                                   statement-1;
                                   statement-2;
                                          ….
                                   Statement-n;
                                    break; 
                    
   case lable2  :
                                   statement-1;
                                   statement-2;
                                           ….
                                   Statement-n;
                                   break; 
  
                                           . . .
 
   default : 
                                     statement-1;
                                     statement-2;
                                          ….
                                     Statement-n;
                                     Break;
 
            }

Execution Process:

Firstly expression is evaluated and depending on the result, concerned statements are executed.   That means, if expression value is equal to label1, then the statements mentioned under case label1 are executed and control comes out of the switch statement. If expression value is not met any one of label value then the statements mentioned under default are executed.

Ex.
 /* this is not complete program. It is partial one */


d=b*b-4*a*c;
if(d<0)
         k=1;
       else
        {
            if(d==0)
            k=2;
        else
        k=3;

switch(k)
{
case 1: System.out.println("roots are imaginary");
               break;
case 2: System.out.println("roots are equal");
               x1=-b/(2*a);
               System.out.println(x1 + “  “ + x1);
               break;
case 3: System.out.println ("roots are unequal");
              x1=-b+sqrt(d)/(2*a);
              x2=-b-sqrt(d)/(2*a);
              System.out.println(x1 + “  “ + x2);
              break;
defualt: System.out.println(" You have entered wrong values");
                 break;
}




iii) ?: operator (Conditional operator)

Systax:


expression 1? expression 2: expression 3;
 
Execution Process:
 
If expression-1 is true then expression-2 is executed and expression-3 is ignored.  If expression-1 is 
false then expression-2 is ignored and expression-3 is executed.  It is some what like if – else statement.
 
Ex:
 
n%2==0 ? System.out.println(“No. is even”) : System.out.println(“No. is odd”);


when the result of n%2==0 is true, the message "No. is even" is displayed or when the result of n%2==0 is false, then the message "No. is odd" is displayed.