0% found this document useful (0 votes)
7 views8 pages

Lab04 C#

lab04 c#

Uploaded by

ahmad bataineh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

Lab04 C#

lab04 c#

Uploaded by

ahmad bataineh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

204111 – Computer and Programming Section 451

Lab #4 – Conditional Statements: Part II

Student ID Name Signature


Sheet’s Owner
Group partner

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;
}

The expression is either an integer- or a string-type expression. Control is transferred to


a specific case according to the value of constant-expression. The embedded statements to be
executed if control is transferred to the case or the default. A break, a jump statement,
transfers control out of the case body. The switch statement can include any number of case
instances, but no two case constants within the same switch statement can have the same
value. Execution of the statement body begins at the selected statements and proceeds until a
break is found.
Example 1.1: A C# program to print out a grade point when a grade is given from the user

Grade Grade point


A 4.0
B 3.0
C 2.0
D 1.0
F 0.0

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

Please input your grade: A


Your point is 4.0.

Lab #4 2 of 8
204111 – Computer and Programming Section 451

Please input your grade: B


Your point is 3.0.

Please input your grade: e


Invalid input!!

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

Please enter a month number: 11


Month: November
Number of days: 30

Please enter a month number: 2


Month: February
Number of days: 28 or 29

Please enter a month number: 7


Month: July
Number of days: 31

Please enter a month number: 20


Invalid input!!

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

Task 2.1: Which number am I pressing?


Write a C# program to translate a letter to a number according to a given mapping table (from
a cell-phone’s dial pad).

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.

Please input a letter: q


The corresponding number of q is 7.

Please input a letter: *


There is no corresponding number for *.

Copy the program into the box provided on page 7.

Task 2.2: Book shipping


Write a C# program to compute the cost of book shipping. The shipping cost is computed
according to the shipping type and the package weight. The shipping rate is show in the
following table.
Shipping types Weight Rate (bahts/gram)
Regular First 2000 0.25
Regular After 2000 0.35
Express Use the same rate as regular + 50 bahts fee
Note that the shipping cost is computed from the possible valid minimum rate. For example,
the shipping cost of a book package of 4.5 kilograms is
cost = (2000 grams × 0.25 bahts/gram) + (2500 grams × 0.35 bahts/gram)
= 500 bahts + 875 bahts = 1375 bahts
Sample outputs:
Enter a shipping type (R-Regular, X-Express): R
Enter the weight of your book package (kilograms): 4.5
Your shipping cost is 1375.00 bahts with R-type shipping of 4.5-kilogram books.

Enter a shipping type (R-Regular, X-Express): Y


Your type is invalid!!

Copy the program into the box provided on page 8

Lab #4 6 of 8
204111 – Computer and Programming Section 451

Space provided for Task 2.1

Lab #4 7 of 8
204111 – Computer and Programming Section 451

Space provided for Task 2.2

Lab #4 8 of 8

You might also like