Tuesday 26 October 2010

switch statement



Definition
Syntax

Example

Decision making are needed when, the program encounters the situation to choose a particular statement among many Statements. If a programmer has to choose one block of statement among many alternatives, nested if...else can be used but, this makes programming logic complex. This type of problem can be handled in C programming using switch statement.

Syntax:

switch (n) {
case constant1:
   code/s to be executed if n equals to constant1;
   break;
case constant2:
   code/s to be executed if n equals to constant2;
   break;
   .
   .
   . 
default:
   code/s to be executed if n doesn't match to any cases;
}

The value of n is either an integer or a character in above syntax. If the value of n matches constant in case, the relevant codes are executed and control moves out of the switch statement. If the n doesn't matches any of the constant in case, then the default codes are executed and control moves out of switch statement.

What is break?
break:

It is a keyword, it belongs to Unconditional statements or jump statements.


Each break statement terminates the enclosing switch statement. Control flow continues with the first statement following the switch block. The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered

Technically, the final break is not required because flow falls out of the switch statement. Using a break is recommended so that modifying the code is easier and less error prone. The default section handles all values that are not explicitly handled by one of the case sections.

What is default ?
It is a keyword.

A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.






SWITCH statement
/*wap to display choice using switch statement*/
#include<stdio.h>
#include<conio.h>
void main()
{
  int a,b,ch;
  clrscr();
  printf("enter any two numbers to perform arithmatic operations\n");
  scanf("%d%d",&a,&b);
  printf("enter your choice\n");
  scanf("%d",&ch);
  printf("1. Addition\n");
  printf("2. Subraction\n");
  printf("3. Multiplication\n");
  printf("4. Division \n");
  switch(ch)
  {

      case 1 :  {
                    printf("Addition is =%d\n",a+b);
                    break;
                    }

      case 2 :  {
                    printf("SUB  is =%d\n",a-b);
                    break;
                    }

      case 3 :  {
                    printf("MULTIPLICATION is =%d\n",a*b);
                    break;
                    }

      case 4 :  {
                    printf("DIVISION =%d\n",a/b);
                    break;
                    }
       default  :  {
                    printf("Invalid Number");
                    break;
                    }
    }
   getch();
}

What is switch block?
The body of a switch statement is known as a switch block. A statement in the switch block can be labeled with one or more case or default labels. The switch statement evaluates its expression, then executes all statements that follow the matching case label.


You may like the following posts:
Keywords
continue
c program on quadratic equation by using switch statement
if..else statement

No comments:

Post a Comment