switch case

Switch case statement evaluates a given expression and based on the evaluated value(matching a certain condition), it executes the statements associated with it. Basically, it is used to perform different actions based on different conditions(cases). 

  • Switch case statements follow a selection-control mechanism and allow a value to change control of execution.
  • They are a substitute for long if statement that compare a variable to several integral values.
  • The switch statement is a multiway branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression.

Syntax: 

switch (n)

{

    case 1: // code to be executed if n = 1;

        break;

    case 2: // code to be executed if n = 2;

        break;

    default: // code to be executed if n doesn't match any cases

}

Flowchart:



1.write a program to provide grocery list to user and also show 

the final calculation of item taken by user

#include<stdio.h>

void main()

{

int a,b,c;

      {

        printf("Grocery shop\n1.Apple\n2.Banana\n3.Cold Drink\n4.Biscuit\n");

        printf("Please Enter the item number of grocery to add in bucket =");

        scanf("%d",&a);

        switch(a)

        {

        case 1:

            printf("Enter the amount of apple in kg=");

            scanf("%d",&b);

            c=b*120;

printf("The cost of %d kg apple is %d\n",b,c);

        break;

        case 2:

            printf("Enter the number of dozen banana's=");

            scanf("%d",&b);

            c=b*50;

printf("The cost of %d dozen Banana's is %d\n",b,c);

        break;

        case 3:

            printf("Enter the number of Cold Drink bottles=");

            scanf("%d",&b);

            c=b*30;

printf("The cost of %d cold drink bottle is %d\n",b,c);

        break;

        case 4:

            printf("Enter the number of biscuit packets=");

            scanf("%d",&b);

            c=b*10;

printf("The cost of %d Biscuit packets is %d\n",b,c);

        break;

        default:

        printf("The Grocerry item number is invalid\n");

        }

    }

}

Output: