Practising if and switch
August 13, 2021 (08:00:10 AM)
Mastering switch statement
Copy-and-paste the following code in a Main method:
Console.WriteLine("Please, enter the day of the week.");
string string_day = Console.ReadLine();
int num_day;
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;
}
Console.WriteLine("The number corresponding to " + string_day + " is " + num_day + ".");Now, do the following:
- Test the program with various values and make sure it behaves as expected.
- Comment 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 being assigned to
num_day. - Change the code so that the days of the week would start on Sunday, i.e., “Sunday” trigger the value 1 to being assigned to
num_day, “Monday” trigger the value 2 to being assigned tonum_day, etc. - Finally, change the last message if the code is in error: use an
ifstatement to display a different message if the user input did not matched one of the literals in yourswitchstatement.
Practicing if and switch
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 practise “transforming” if statements into switch statements, and reciprocally. This will help you in memorizing both, and in chosing the most convenient to perform certain task.
Create a new project and do the following in Main.
- Initialize a
stringvariable named “day,” anintvariable named “myVar,” acharvariable named “initial,” and a Boolean variable named “flag.” - 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 help you in making sure that your statements behave as they are supposed to.
From switch to if-else
- Write a
switchstatement that setsflagtotrueif the value ofdayis"Mon.","Tue.","Wed.","Thu."or"Fri.", and tofalseotherwise. - Rewrite the previous statement as an
if-elsestatement.
From if-else to switch
- Write a
if-elsestatement that doubles the value ofmyVarifmyVaris3,5or7. - Can you rewrite the previous statement as a
switchstatement? If so, do it. If not, explain why not.
Deciding Between Condition Types
- Write a statement that doubles the value of
myVarand setsinitialto'M'ifdayis 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
initialis'E'or'e', “Bonjour” if the value ofinitialis'F'or'f', “Guten Tag” if the value ofinitalis'D'or'd'. What is the appropriate kind of statement to do this?
Complex Conditions
- Write a statement that doubles the value of
myVarifdayis"Sun.", triples the value ofmyVarifdayis not"Sun."andinitialis'a', and setsmyVarto0otherwise. - Write a statement that sets
myVarto0ifinitialis an upper-case letter, and to1otherwise. You will need to understand how to use theIsUppermethod, and the documentation can help you with that.