Lab04 C#
Lab04 C#
1. switch...case Statements
switch statement is a control statement that handles multiple selections. It passes control to
one of the case statements within its body.
switch (expression )
{
case constant-expression-1 :
statements ;
break;
case constant-expression-2 :
statements ;
break;
case constant-expression-3 :
statements ;
break;
:
default:
statements ;
break;
}
Lab #4 1 of 8
204111 – Computer and Programming Section 451
1 using System ;
2 class ExSWITCHC {
3 static void Main () {
4 string grade ;
5 Console . Write ( " Please input your grade : " );
6 grade = Console . ReadLine ();
7 switch ( grade ) {
8 case " A " : Console . WriteLine ( " Your point is 4.0. " ); break ;
9 case " a " : Console . WriteLine ( " Your point is 4.0. " ); break ;
10 case " B " : Console . WriteLine ( " Your point is 3.0. " ); break ;
11 case " b " : Console . WriteLine ( " Your point is 3.0. " ); break ;
12 case " C " : Console . WriteLine ( " Your point is 2.0. " ); break ;
13 case " c " : Console . WriteLine ( " Your point is 2.0. " ); break ;
14 case " D " : Console . WriteLine ( " Your point is 1.0. " ); break ;
15 case " d " : Console . WriteLine ( " Your point is 1.0. " ); break ;
16 case " F " : Console . WriteLine ( " Your point is 0.0. " ); break ;
17 case " f " : Console . WriteLine ( " Your point is 0.0. " ); break ;
18 default : Console . WriteLine ( " Invalid input !! " ); break ;
19 }
20 }
21 }
However, the above program is quite lengthy. C# allows us to combine multiple cases with the
same statement body together, as shown below.
1 using System ;
2 class ExSWITCH1C {
3 static void Main () {
4 string grade ;
5 Console . Write ( " Please input your grade : " );
6 grade = Console . ReadLine ();
7 switch ( grade ) {
8 case " A " :
9 case " a " : Console . WriteLine ( " Your point is 4.0. " ); break ;
10 case " B " :
11 case " b " : Console . WriteLine ( " Your point is 3.0. " ); break ;
12 case " C " :
13 case " c " : Console . WriteLine ( " Your point is 2.0. " ); break ;
14 case " D " :
15 case " d " : Console . WriteLine ( " Your point is 1.0. " ); break ;
16 case " F " :
17 case " f " : Console . WriteLine ( " Your point is 0.0. " ); break ;
18 default : Console . WriteLine ( " Invalid input !! " ); break ;
19 }
20 }
21 }
Sample outputs
Lab #4 2 of 8
204111 – Computer and Programming Section 451
Example 1.2: A C# program to print out the name and number of days in the given month
number
1 using System ;
2 class ExSWITCH2C {
3 static void Main () {
4 int month_number =0;
5 Console . Write ( " Please enter a month number : " );
6 month_number = int . Parse ( Console . ReadLine ());
7 switch ( month_number ) {
8 case 1 : Console . WriteLine ( " Month : January " );
9 Console . WriteLine ( " Number of days : 31 " );
10 break ;
11 case 2 : Console . WriteLine ( " Month : February " );
12 Console . WriteLine ( " Number of days : 28 or 29 " );
13 break ;
14 case 3 : Console . WriteLine ( " Month : March " );
15 Console . WriteLine ( " Number of days : 31 " );
16 break ;
17 case 4 : Console . WriteLine ( " Month : April " );
18 Console . WriteLine ( " Number of days : 30 " );
19 break ;
20 case 5 : Console . WriteLine ( " Month : May " );
21 Console . WriteLine ( " Number of days : 31 " );
22 break ;
23 case 6 : Console . WriteLine ( " Month : June " );
24 Console . WriteLine ( " Number of days : 30 " );
25 break ;
26 case 7 : Console . WriteLine ( " Month : July " );
27 Console . WriteLine ( " Number of days : 31 " );
28 break ;
29 case 8 : Console . WriteLine ( " Month : August " );
30 Console . WriteLine ( " Number of days : 31 " );
31 break ;
32 case 9 : Console . WriteLine ( " Month : September " );
33 Console . WriteLine ( " Number of days : 30 " );
34 break ;
35 case 10: Console . WriteLine ( " Month : October " );
36 Console . WriteLine ( " Number of days : 31 " );
37 break ;
38 case 11: Console . WriteLine ( " Month : November " );
39 Console . WriteLine ( " Number of days : 30 " );
40 break ;
41 case 12: Console . WriteLine ( " Month : December " );
42 Console . WriteLine ( " Number of days : 31 " );
43 break ;
44 default : Console . WriteLine ( " Invalid input !! " );
45 break ;
46 }
47 }
48 }
Lab #4 3 of 8
204111 – Computer and Programming Section 451
Sample outputs
Exercise 1.1: Type the following program into SharpDevelop, and answer the ques-
tions
1 using System ;
2 class Coffee {
3 static void Main () {
4 int add_up ;
5 double cost = 35.0;
6 Console . WriteLine ( " You can add into your coffee with either : " );
7 Console . WriteLine ( " 1 - Milk , 2 - Sugar , 3 - Milk + Sugar , 4 - Nothing " );
8 Console . Write ( " Your choice is : " );
9 add_up = int . Parse ( Console . ReadLine ());
10 switch ( add_up ) {
11 case 1 : cost += 9.00;
12 break ;
13 case 2 : cost += 5.00;
14 break ;
15 case 3 : cost += 12.00;
16 break ;
17 default : cost += 0.0;
18 break ;
19 }
20 Console . WriteLine ( " Your coffee cost is {0} baht . " , cost );
21 Console . WriteLine ( " Thank you . " );
22 }
23 }
• What are the input values of add up to make the value of cost less than 45?
Lab #4 4 of 8
204111 – Computer and Programming Section 451
• If the user enters 1 to the program, what will be the resulting value of cost?
• If the user enters 4 to the program, what will be the results displayed?
• If the user enters -1 to the program, what will be the results displayed?
• Modify the switch statement and the last two display statements so that the program
can handle invalid inputs from the user. If the user enters an invalid input, the program
should set cost to zero and display to the user that “Your coffee is canceled.” Write
the modified statements in the box provided below.
Lab #4 5 of 8
204111 – Computer and Programming Section 451
2. Programming Exercises
Letters Number
ABC 2
DEF 3
GHI 4
JKL 5
MNO 6
PQRS 7
TUV 8
WXYZ 9
Sample outputs:
Please input a letter: A
The corresponding number of A is 2.
Lab #4 6 of 8
204111 – Computer and Programming Section 451
Lab #4 7 of 8
204111 – Computer and Programming Section 451
Lab #4 8 of 8