This lab serves multiple goals:
if statements,switch statements,switch or if should
be used,ToUpper method.switch statementConsider the following code:
// We ask the user to enter the day of the week:
Console.WriteLine("Please enter the day of the week.");
// Note that anything that is not spelled exactly as
// in the switch statement will be treated as the
// default case.
// We read from the user:
string string_day = Console.ReadLine();
// Variable where the result of our computation will be stored:
int num_day;
// Switch statement to map textual description of the day
// (e.g., "Monday", "Tuesday", etc.) to a number
// relative to chronological order (e.g., 1, 2, etc.)
switch (string_day)
{
case ("Monday"):
num_day = 1;
break;
case ("Tuesday"):
num_day = 2;
break;
case ("Wednesday"):
num_day = 3;
break;
case ("Thursday"):
num_day = 4;
break;
case ("Friday"):
num_day = 5;
break;
case ("Saturday"):
num_day = 6;
break;
case ("Sunday"):
num_day = 7;
break;
default:
num_day = -1; // This is an error code.
break;
}
// We display the number corresponding to the day entered:
Console.WriteLine("The number corresponding to " + string_day + " is " + num_day + ".");
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:
default: case along with the two lines below it
and compile your program. Why is the compiler complaining?num_day.num_day,
“Monday” causes the value 2 to be assigned to num_day, etc.)if statement to display a different
message if the user input did not match one of the literals in your
switch 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 ↵:
Please enter the day of the week.
t͟u͟e͟s͟d͟a͟y͟↵
The number corresponding to tuesday is 3.
Here is a second example:
Please enter the day of the week.
M͟O͟N͟D͟A͟Y͟↵
I am sorry, but MONDAY does not seem to be a valid day.
You can find an example solution in this solution or in the archive that you downloaded previously.
if and switchThis 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.
string variable named dayint variable named myVarchar variable named initial, andbool variable named flagswitch to if-elseswitch statement that sets flag to true if the value
of day is "Mon", "Tue", "Wed", "Thu" or "Fri", and to
false otherwise.if-else statement.if-else to switchif-else statement that doubles the value of myVar if
myVar is: 3, 5, or 7.switch statement? If
so, do it. If not, explain why not.myVar and sets
initial to 'M' if day is equal to "Sat". What is the
appropriate kind of statement to do this?initial is 'E' or 'e', “Bonjour” if the value of initial
is 'F' or 'f', and “Guten Tag” if the value of initial 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.
myVar if day is
"Sun", triples the value of myVar if day is not "Sun" but
initial is 'a', or sets myVar to 0 if neither of the other
conditions is satisfied.myVar to 0 if initial is an
upper-case letter or to 1 otherwise. You will need to understand
how to use the IsUpper method, and the
documentation
can help you with that.Note that you can only solve those problems with if statements.
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:
myVar to 0 if initial is an
upper-case letter, and to 1 otherwise. You already wrote an if
statement that accomplishes this in the previous exercise, so you
just need to rewrite it using the conditional operator.initial to 'B' if myVar is greater
than 500 and to 'S' if myVar is less than or equal to 500.myVar if day is
"Sat" or "Sun" and adds 1 to the value of myVar otherwise.ToUpper() MethodC## 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
Console.WriteLine("Hello, world!".ToUpper());
will display
HELLO, WORLD!
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.