Docstoc

If Else and Switch Case Statement

Document Sample
If Else and Switch Case Statement Powered By Docstoc
					Decision Making
switch - case Statement
The switch - case statement is used to select one of the various options available i.e. when the outcome is
categorised in different groups switch - case is often used. The syntax of the switch - case statement is
as follows :

     switch(expression)
      {
          case label1: ...........statement.........
                       break;
          case label2: ...........statement.........
                       break;
          case label3: ...........statement.........
                       break;
       }




The simple if statement has the following syntax :

     if(condition satisfies)
     {
       ..........statement...........
       ..........statement...........
       ..........statement...........
      }


The if statement is a decision making statement. It is a straight - forward statement which allows the code
inside the if block to be executed only when the condition is satisfied. Example


     if(num < 0)
     {
         System.out.println("The number is negative");
      }

Here the if statement checks whether the number is less than 0, if the condition is satisfied the print statement
inside is executed.

Simple if Statement
The if - else statement is similar to the simple if statement. In this case when the condition is satisfied the
code inside the if block gets executed and the else block is ignored. Otherwise the control is shifted to
the else block and the code inside is executed. Example


     if(num < 0)
     {
         System.out.println("The number is negative.");
      }

     else
     {
             System.out.println("The number is positive.");
         }

When the value of num is less than 0 the if statement being satisfied, it is executed.
Otherwise straight away the else statement is executed.

if - else Statement
A nested if statement is nothing but an if statement inside an if statement. The following example should make
it clear.


     if(num1 > num2)
     {
         if(num1 > num3)
         {
             System.out.print(num1);
          }
     }


The example below will clarify all the above three concepts.

// To find the largest among three numbers //


     if(num1 > num2)
     {
         if(num1 > num3)
         {
             System.out.print(num1);
          }

             else
             {
                    System.out.print(num3);
             }
         }

     else
     {
         if(num2 > num3)
         {
             System.out.print(num2);
          }

             else
             {
                    System.out.print(num3);
             }
         }
Here's an example which uses switch - case statement.


// The code below displays the day according to the value of choice //

         switch(choice)
         {
              case 1 : System.out.print("Sunday");
                        break;
              case 2 : System.out.print("Monday");
                        break;
              case 3 : System.out.print("Tuesday");
                        break;
              case 4 : System.out.print("Wednesday");
                      break;
             case 5 : System.out.print("Thursday");
                      break;
             case 6 : System.out.print("Friday");
                      break;
             case 7 : System.out.print("Saturday");
                      break;
             default: System.out.print("Invalid Choice !");
       }


It is to be noted that a break statement is followed after every case so because it indicates that the particular
case ends after the break statement and the control is transferred to the next outside the switch - case statement.
In the absence of the break statement, all the cases after the chosen case will also be executed as the control is
transferred to the next case in absence of the break statement which causes an undesired output.

The default case is used to display an error message in case the value of the expression in the switch
statement does not statisfy with any of the cases mentioned.

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:30
posted:6/21/2010
language:English
pages:3
Description: The use of If - Else statement and Switch case statement is shown.