Pages

Friday, 7 July 2017

Switch Case C Language

Switch Case


A switch case is uncomplicated solution/alternate of complicated nested if-else. It is useful when there is a need check to arise multiple possibility on single variable like print day of the week name based on the given day number. Best for long if statements that compare a variable to several "integral" values ("integral" values are simply values that can be expressed as an integer, such as the value of a char).

Switch case syntax

Above figure is showing syntax of switch case. In syntax break and default are in bracket, which means they are optional. You can use more than one case without using break. If break is omitted, then all the ca
se code will be run after matched case from the given number.
If default is omitted then you cannot trace user input if any case not matched.

/* #######    Girfa Student Help ###############
      Switch case Program
      for more program visit : http://girfahelp.blogspot.com/p/ebook-c-langauge.html
  ##############################################  */
#include<stdio.h>
void main()
{
    int day;
    clrscr();
    printf(" Enter day of the week>> ");
    scanf("%d",&day);
    switch(day)
    {
      case 1:
            puts("Mon");
            break;
      case 2:
            puts("Tue");
            break;
      case 3:
            puts("Wed");
            break;
      case 4:
            puts("Thu");
            break;
      case 5:
            puts("Fri");
            break;
      case 6:
            puts("Stu");
            break;
      default:
            puts("Invalid Choice");
    }
    getch();

 }

Skkiping break;



Program : write a program to check whether a input character is vowel or constant?

Solution :

/* #######    Girfa Student Help ###############
      Switch case Program
      for more program visit : http://girfahelp.blogspot.com/p/ebook-c-langauge.html
  ##############################################  */
#include<stdio.h>
void main()
{
    char ch;

    clrscr();
    printf(" Enter day of the character>> ");
    scanf("%c",&ch);
    switch(ch)
    {
      case 'a':
      case 'e':
      case 'o':
      case 'u':
      case 'i':
      {
            puts("Vovel");
            break;
      }
      default:
            printf("Not a valid vowel constant");
    }
    getch();


 }

No comments:

Post a Comment