What season is this month in?
Select the button below to open the C# program in a new window. Run the program and read the lines of code to see if you can understand how it works. It will be helpful to arrange your display so that you can have this browser window on one side of the screen and the code on the other.
Watch this video to learn about the new concepts shown in the program:
The new commands used in this program and others that may be useful. Select them below to learn more:
switch
An alternative to the if/else if/else structure that is used to select one of many code blocks to be executed. Switch is considered a better command to use than the if/else if/ else structure when there are many outcomes because it is more readable for multiple values.
case
Is the value for the match command. You can have as many case statements as you need. Don’t forget the colon at the end of the case statement.
default
The equivalent of `else`. It captures anything else that was not matched by the previous case statements. Don't forget the colon at the end of the default statement.
Questions to think about with this program to check your understanding:
In line 48: `Console.WriteLine($"Month {month} is in the {season}");` why is the word month written twice in this line, but it is only output once?
Month is a string, it is the actual word, "Month". In contrast, {month} is a variable: an area in memory to hold data that can change. This is holding the name of the month the user inputs. E.g. "May". As we want the word "Month" followed by the name of the month to be output we need both in the statement.
How could lines 12-16 be written using an `if` instead of `switch`?
if (month == "December") || (month == "January") || (month == "February"):
Change the program so that:
Enter the number of the month: 5
Month 5 is in the Spring
Use the flowcharts if you need more support in understanding the steps required to meet the criteria. Use the Parsons code sorting exercise only if you are really stuck.
Run your code to check that your program has met the success criteria and produces the required output.
Enter the month: 1
Month 1 is in the Winter
Enter the month: September
Month September is in the Autumn
Enter the month: 8
Month 8 is in the Summer