Switch Statement in C with Programs and Examples

Switch statement in C: If several options are available then it isn’t recommended to take nested if-else we should go for switch statement so that readability of the code will be improved. “Switch” is a keyword, by using which we can create a selection statement with multiple choices. Multiple choices can be constructed by using a “Case” keyword.

Syntax:

Switch Syntax:
Switch(expression)
{
 case exp1:
 statements;
 break;
 case exp2:
 statements;
 break;
 …………….
 …………….
 …………….
 case expression:
 statements;
 break;
 default:
 statements;
}
Explanation: In the above syntax, switch, case, break are keywords. Expr1, expr2 is known as “case labels”. Statements inside case expression need not be closed in braces. The break statement causes an exit from the switch statement.

The following rules apply to a switch statement:

1) The expression used in a switch statement must have an integral or enumerated type.
2) You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
3) The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.
4) When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
5) When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
6) Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
7) A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

Switch statement in C Program 1 :

C Switch program Example:
#include<stdio.h>
void main()
{
 int number;
printf("Enter a number");
 scanf("%d",&number);
 switch(number)
 {
  case 1:
  printf("Welcome to TutorialWeb.org");
  break;
  case 2:
  printf("I am Java programmer");
  break;
  case 3:
  printf("Java programming in TutorialWeb.org");
  break;
  default:
  printf("Switch Program Example");
  break;
 }
}
Output:
Enter a number
1
Welcome to TutorialWeb.org

Explanation:

1) The integer value “1” is assigned to a number variable.
2) After switch case decides then 1st case block of code gets executed.
3) Switch case executes code from top to bottom. It will validate the case number with the variable number.
4) If no match is found then it will jump to the next case. It checks until found if the case is not found, then the default will be executed.

Switch statement in C Program 2 :

C Switch program Example:
#include<stdio.h>
void main()
{
 int number;
printf("Enter a number");
 scanf("%d",&number);
 switch(number)
 {
  case 1:
  printf("January");
  break;
  case 2:
  printf("February");
  break;
  case 3:
  printf("March");
  break;
  case 4:
  printf("April");
  break;
  case 5:
  printf("May");
  break;
  case 6:
  printf("June");
  break;
  case 7:
  printf("July");
  break;
  case 8:
  printf("August");
  break;
  case 9:
 printf("September");
  break;
  case 10:
  printf("October");
  break;
  case 11:
 printf("November");
  break;
  case 12:
  printf("December");
  break;
  default:
  printf("Enter number between 1 to 12");
  break;
 }
}
Output:
Enter a number
7
July
Read Also C Loops: Loops in C
Share:

Ads

Search This Blog

  • ()
Powered by Blogger.

Strings in C With Syntax, C String Program with output

Strings in C :  Strings can be defined as the one-dimensional array of characters terminated by a null ('\0'). The difference betwee...

Blog Archive