0
2.5kviews
Explain syntax of switch case? Discuss what is need of break statement in switch case? Write a program to court vowel using switch case.
1 Answer
0
47views

Syntax

switch(expression){

case constant-expression  :
      statement(s);
break;/* optional */

case constant-expression  :
      statement(s);
break;/* optional */

/* you can have any number of case statements */
default:/* Optional */
   statement(s);
}
Need of ‘break;’
If the break statement is not provided, the compiler will execute all the cases following the selected case.
Program to find if entered character is a vowel or not
#include <stdio.h>

void main()
{
  char ch;

  printf("Input a character\n");
  scanf("%c", &ch);

  switch(ch)
  {
    case 'a':
    case 'A':
    case 'e':
    case 'E':
    case 'i':
    case 'I':
    case 'o':
    case 'O':
    case 'u':
    case 'U':
      printf("%c is a vowel.\n", ch);
break;
    default:
      printf("%c is not a vowel.\n", ch);
  }              
   getche();
}
Please log in to add an answer.