Thursday, July 14, 2011

SWITCH-CASE STATEMENT

SWITCH-CASE STATEMENT

Switch Statements

The switch and case statements help control complex conditional and branching operations. The switch statement transfers control to a statement within its body.
Syntax:
The general form of a switch statement is:
switch (variable)
{
    case expression1:
        do something 1;
        break;
    case expression2:
        do something 2;
        break;
      ....
    default:
        do default processing;
}

Example :
#include<stdio.h>
 
main()
{
    int x;
 
    printf("Enter the any number");
    scanf("%d", &x);
 
    switch(x)
{
 case 1:
    printf("I am in Case one");
    break;
 case 2:
    printf("I am in case two");
    break;
 case 3:
    printf("I am in case three");
    break;
 default:
    printf(" I am in Default");
}
}
 
In above program if we enter 1 value in x then the output will be:
I am in Case one
When an expression is found that is equal to the tested variable, execution continues until a break statement is encountered. It is possible to have a case without a break. This causes execution to fall through into the next case. This is sometimes very useful.

0 comments:

Post a Comment