Thursday, December 22, 2011

SWITCH CASE STATEMENTS

Introduction

In short instead of using many if else statements in a program we can use Switch Case statements.
For example:

#include"stdio.h"
main()
{
    int a;

    printf("Just Enter a Number : ");
    scanf("%d",&a);

    if ( a == 1)
        printf("You Entered 1 ! \n");

    else if ( a == 2)
        printf("You Entered 2 ! \n");

    else if ( a == 3)
        printf("You Entered 3 ! \n");

    else if ( a == 4)
        printf("You Entered 4 ! \n");

       /* So On.............
       .......................*/

    else
        printf("Your Entry was Wrong !\n");
}

We can also write the program in the following way :

#include"stdio.h"
main()
{
    int a;

    printf("Just Enter a Number : ");
    scanf("%d",&a);

    switch(a)
    {
    case 1:
        printf("You Entered 1 ! \n");
        break;

    case 2:
        printf("You Entered 2 ! \n");
        break;

    case 3:
        printf("You Entered 3 ! \n");
        break;

    case 4:
        printf("You Entered 4 ! \n");
        break;

    default:
        printf("Your Entry was Wrong ! \n");
        break;       
    }
}

Syntax for switch case is :

switch ( integer variable )
    {

    case ( option 1 ) :

            statement 1;
            statement 2;
            break;

    case ( option 2 ):

            statement 1;
            statement 2;
            break;

    case ( option 3 ) :

            statement 1;
            statement 2;
            break;

    case ( option 4 ) :

            statement 1;
            statement 2;
            break;

    default :
            default statement;
            break;

    }

Here the options under cases are dependent on the integer variable under switch.
If the value of the variable matches with the case value then the statements under that case will be executed.
For example :

#include"stdio.h"
main()
{
    switch ( 65 )
    {
    case 32:
        printf("Case 32 !\n");
        break;

    case 65:
        printf("This one will be executed !\n");
        break;

    case 100:
        printf("Case 100 !\n");
        break;
    }
}

The Output will be : This one will be executed !

break has to be written under every statements inside a case. If we do not put it then the next case statement will also be executed.
For example :

#include"stdio.h"
main()
{
    switch ( 65 )
    {

    case 32:
        printf("Case 32 !\n");
        break;

    case 65:
        printf("This one will be executed !\n");
        // Without break;

    case 100:
        printf("Case 100 !\n");
        break;
    }
}

The Output will be : This one will be executed !
                              Case 100 !

If no values are matched with switch and case then the statement under default will be executed.
For example :

#include"stdio.h"
main()
{
    switch ( 2 )
    {
    case 32:
        printf("Case 32 !\n");
        break;

    case 65:
        printf("This one will be executed !\n");
        break;

    case 100:
        printf("Case 100 !\n");
        break;
   
    default:
        printf("Default statement !\n");
        break;
    }
}

The Output will be : Default statement !

Few More Examples

#include"stdio.h"
main()
{
    int a,b,c,r;

    printf("Enter 2 Numbers:");
    scanf("%d%d",&a,&b);

    printf("\n1...ADD:");
    printf("\n2...SUB:");
    printf("\n3...MUL:");
    printf("\n4...DIV:");

    printf("\nEnter your choice (1/2/3/4) : ");
    scanf("%d",&c);

    switch(c)
    {
    case 1: r=a+b;
        printf("ANSWER = %d\n",r);
        break;

    case 2: r=a-b;
        printf("ANSWER = %d\n",r);
        break;

    case 3: r=a*b;
        printf("ANSWER = %d\n",r);
        break;

    case 4: r=(float)a/b;
        printf("ANSWER = %d\n",r);
        break;

    default:
        printf("Wrong Entry !\n");
        break;
    }
}

A Very Useful Program :

#include"stdio.h"
main()
{
    int flag=1;

    while(flag==1)
    {

        static int totalbalance=100;
        int deposit,withdraw,cal;

        printf("\n1...DEPOSIT");
        printf("\n2...WITHDRAWL");
        printf("\n3...ENQUIRY");
        printf("\n4...EXIT \n");   

        printf("\nPlease Enter Your Choice (1/2/3/4) : ");
        scanf("%d",&cal);

        switch(cal)
        {
        case 1:

            printf("\nDEPOSIT \n");
            printf("ENTER AMOUNT : ");
            scanf("%d",&deposit);
            totalbalance=totalbalance+deposit;
            printf("TOTAL BALANCE = %d\n",totalbalance);   
            break;

        case 2:

            printf("\nWITHDRAWL \n");
            printf("ENTER AMOUNT : ");
            scanf("%d",&withdraw);
       
            if(withdraw>totalbalance)
                printf("\nYOU DO NOT HAVE ENOUGH BALANCE !\n");
            else
            {
                totalbalance=totalbalance-withdraw;
                printf("\nTOTAL BALANCE = %d\n",totalbalance);
            }
            break;

        case 3:

            printf("\nTOTAL BALANCE = %d\n",totalbalance);
            break;

        case 4:
            printf("\nTHANK YOU !\n\n");
            flag=0;
            break;

        default:
            printf("\nWRONG ENTRY !\n");
            break;   
        }
    }
}


No comments:

Post a Comment

Followers