Use switch statement to check case of user input - C Statement

C examples for Statement:switch

Description

Use switch statement to check case of user input

Demo Code

#include <stdio.h>

int main(void)
{
  char answer = 0;       // Stores an input character

  printf("Enter Y or N: ");
  scanf(" %c", &answer);

  switch(answer)/*from   ww w.j ava  2s  .  c o  m*/
  {
    case 'y': case 'Y':
      printf("You responded in the affirmative.\n");
      break;

    case 'n': case 'N':
      printf("You responded in the negative.\n");
      break;
        default:
          printf("You did not respond correctly. . .\n");
          break;
   }
   return 0;
}

Result


Related Tutorials