Monday 23 January 2017

Control Statements in Java ( Selection Statements )


                                                 
Control Statements in Java ( Selection Statements )

 Selection Statements

 Java supports 2 selection statements if and switch. These statements allow you to control the flow of your program 's execution based upon conditions known only during run-time.

Simple if

If is a java's conditional branch statement i.e you need to state the condition along with this. It's general form is : 

       if ( condition ) statement1 ; 

Program:

 // To demonstrate if clause

 int a=10 , b=20 ;
 if ( a > b )
{
a=0;                                                            Output-
}                                                                  a=10 , b=0
b=0;          

here the control will go inside the block , check the condition which is false and exit the block. but it will change the value of b to 0.

if-else

The if part is same but the else part is optional. it works like this , if the condition is true the if part will be executed and if the else condition is true then the  next block i.e else block will be executed.

The general form is : 
  
           if ( Condition )
{  // If block 
}
 else
{
 //else block
}

Program:

 // to demonstrate if-else clause
 int =10 , b=20;

if ( b >a )
{
 a=100;
 b=200;
}
else                                                           Output:
{                                                                   a=100 , b=200;
 a= b=0;
}

here since the condition is true so the control enters the block and overwrites the value of  a and b  to 100 and 200 resp.


Nested If

A nested if is an if statement that is the target of another if and else. They are quite commonly used.

The  general form is :

                     if ( condition 1)
 
                     if ( condition 2 )
         {
                 }
                     else
                  {
                  }
  }
 else
{
}

Program:

  // to demonstrate nested if

    if ( i==10 )
{
  if ( j<20) 
  a=b;
  if ( k < 100 )
 c=d;
else
 a=c;
}
else
a=d;


Else-if Ladder


It is the common programming construct which is based on previous nested if 's criteria. The general form is:

     if ( condition )
   statement ;
 else if ( condition )
 statement ;
else if ( condition )
 statement ;
.
.
.
else
 statement ;

program:

// To demonstrate else-if ladder

  class IfElse
{
    public static void main ( String args [] )
 {
    int month = 4 ;                              // April
     String season ;

  if ( month == 12 || month == 1 || month == 2 )
      season = " Winter " ;
else if ( month ==3 || month == 4 || month == 5 )
      season = "Spring" ; 
else if ( month== 6 || month == 7 || month == 8 )
      season = "Summer " ;
else if ( month = 9 || month ==10 || month ==11 )
      season = " Autumn " ;
else
  season = " Bonus month , enjoy it fella's " ;

System.out,println ( " April is in the " + season + " . ");
     }
}

       Output:
   April is in spring.


Switch

The switch is java's multi way branch statement. It provides an easy way to dispatch execution to different parts of your code based on the value of an expression. It's  general form is:
    switch ( expression )
{
    case value1 :
     //statement sequence
      break;
case value2 :
   //statement sequence
   break;
.
.
.
case valueN :
    //statement sequence
    break;
 default :
  // default statement sequence
}

here the expression must be of the type  byte , short , int , char ; each of the values specified in the case statements must be of type compatible with the expression. Each case value must be unique literal.

Program:

// To demonstrate switch

class SimpleSwitch
{
   public static void main ( String args [] )
  {
    for ( int i=0; i<6 ; i++)
   switch(i)
  { 
    case 0 :
     System.out.println (" i is Zero. ");
    break;
   case 1: 
  System.out.println (" i is One. ");
   break;
   case 2 :
   System.out.println (" i is Two. ");
    break;
    case 3 :
   System.out.println (" i is Three. ");
   break;
  default :
  System.out.println (" i is greater than 3. ");
         }
       }
   }
                              Output:
                             i is Zero.
                             i is One.
                             i is Two.
                             i is Three.
                             i is greater than 3.
                             i is greater than 3.

                                                                        By: Knowledge Bits

No comments:

Post a Comment

Video of the Day

Contact us

Name

Email *

Message *