This lab serves multiple goals:
- To reinforce your understanding of
if
statements, - To help you practice
switch
statements, - To help you understand the instances in which
switch
orif
should be used, - (Optional) to help you understand the conditional operator, and
- (Optional) to introduce you to the
ToUpper
method.
Mastering the switch
statement
Consider the following code:
You can download it as a solution
(that also contains the solution to the problems asked below. Make sure
you open day_of_the_week
and not day_of_the_week_solution
first).
Now, do the following:
- Test the program with various values and make sure it behaves as expected.
- Comment out the
default:
case along with the two lines below it and compile your program. Why is the compiler complaining? - Restore the code to its original state.
- Change the code so that “monday” would make the value 1 get assigned
to
num_day
. - Change the code so that the days of the week start on
Sunday
(i.e., “Sunday” causes the value 1 to get assigned to
num_day
, “Monday” causes the value 2 to be assigned tonum_day
, etc.) - Finally, change the last message to tell the user if the code
encountered an error; use an
if
statement to display a different message if the user input did not match one of the literals in yourswitch
statement.
Here is an example of execution, where the user input is u͟n͟d͟e͟r͟l͟i͟n͟e͟d͟, and hitting “enter” is represented by “⏎͟”:
Here is a second example:
You can find an example solution in this solution or in the archive that you downloaded previously.
Practicing if
and switch
{#practicing-if-and-switch-1}
This exercise will ask you to write a rather abstract program that
performs simple manipulations on a few variables. The main goal is to
have you practice “transforming” if
statements into switch
statements and switch
statements into if
statements. This will help
you memorize their syntax and help you choose the more convenient one to
perform certain tasks.
Create a new project and do the following in Main
.
- Declare and initialize the following variables:
- a
string
variable namedday
- an
int
variable namedmyVar
- a
char
variable namedinitial
, and - a
bool
variable namedflag
- a
- Set and change the value of these variables to make good tests as you progress through this problem.
- You can also display them on the screen to make sure that your statements behave as expected.
From switch
to if-else
- Write a
switch
statement that setsflag
totrue
if the value ofday
is"Mon"
,"Tue"
,"Wed"
,"Thu"
or"Fri"
, and tofalse
otherwise. - Rewrite the previous statement as an
if-else
statement.
From if-else
to switch
- Write an
if-else
statement that doubles the value ofmyVar
ifmyVar
is:3
,5
, or7
. - Can you rewrite the previous statement as a
switch
statement? If so, do it. If not, explain why not.
Deciding Between Condition Types
- Write a statement that doubles the value of
myVar
and setsinitial
to'M'
ifday
is equal to"Sat"
. What is the appropriate kind of statement to do this? - Write a statement that displays “Hello” on the screen if the value
of
initial
is'E'
or'e'
, “Bonjour” if the value ofinitial
is'F'
or'f'
, and “Guten Tag” if the value ofinitial
is'D'
or'd'
. What is the appropriate kind of statement to do this?
Note that you can solve those problems with either an if
statement or
a switch
statement.
Complex Conditions
- Write a statement that doubles the value of
myVar
ifday
is"Sun"
, triples the value ofmyVar
ifday
is not"Sun"
butinitial
is'a'
, or setsmyVar
to0
if neither of the other conditions is satisfied. - Write a statement that sets
myVar
to0
ifinitial
is an upper-case letter or to1
otherwise. You will need to understand how to use theIsUpper
method, and the documentation can help you with that.
Note that you can only solve those problems with if
statements.
Pushing Further (Optional)
Conditional Operator
A conditional operator can be used to replace if-else
statements in
particular cases (i.e., assignment, call, increment, decrement, and new
object expressions). Its structure is:
condition ? first_expression : second_expression;
You can read more about it in the documentation.
Practice using the conditional operator by adding these statements to the program you developed previously:
- Write a statement that sets
myVar
to0
ifinitial
is an upper-case letter, and to1
otherwise. You already wrote anif
statement that accomplishes this in the previous exercise, so you just need to rewrite it using the conditional operator. - Write a statement that sets
initial
to'B'
ifmyVar
is greater than 500 and to'S'
ifmyVar
is less than or equal to 500. - Write a statement that doubles the value of
myVar
ifday
is"Sat"
or"Sun"
and adds 1 to the value ofmyVar
otherwise.
ToUpper()
Method
C# contains a method called ToUpper()
in the string
class. You can
read its
documentation,
but the simplest way to understand is probably to see an example first.
The statement
will display
Can you use this method to make your switch
statement from the first
part accommodate any combination of
uppercase and lowercase letters for the days of the week? If done
properly, your program should then correctly identify that “MoNdAy”,
“MONDAY”, “monday” and “Monday” all match the same value.