1switch (n)
2{
3 case 1: // code to be executed if n = 1;
4 break;
5 case 2: // code to be executed if n = 2;
6 break;
7 default: // code to be executed if n doesn't match any cases
8}
1#include <iostream>
2using namespace std;
3
4int main () {
5 // local variable declaration:
6 char grade = 'D';
7
8 switch(grade) {
9 case 'A' :
10 cout << "Excellent!" << endl;
11 break;
12 case 'B' :
13 case 'C' :
14 cout << "Well done" << endl;
15 break;
16 case 'D' :
17 cout << "You passed" << endl;
18 break;
19 case 'F' :
20 cout << "Better try again" << endl;
21 break;
22 default :
23 cout << "Invalid grade" << endl;
24 }
25 cout << "Your grade is " << grade << endl;
26
27 return 0;
28}
1var = 1
2
3switch (var):
4
5 case 1:
6 break; // Code that is executed if var is 1
7
8 case 2:
9 break; // Code that is executed if var is 2
10
11 default:
12 break; // Code that is executed if no cases match
13