Drop links or images here to add them to the editor.

What season is this month in?

Seasons

Try

Watch this video to learn about the new concepts shown in the program:

Knowledge organiser

The new commands used in this program and others that may be useful. Select them below to learn more:

switch, case, default

switch

Provides an alternative structure to if, else if, else and is used in situations where there are more than two possibilities. It is considered a better choice than if/else if/else when there are many outcomes, because it is more readable. The expression to match goes between parentheses, and the cases are enclosed in braces.

case

Is the value for the switch command. You can have as many case labels as you need, and don’t forget the colon at the end of each one (case x:). Multiple case labels with no statements between them all map to the same body, so you can list several values that should produce the same result. End each case body with return or break.

default

The equivalent of else: a special label that captures anything else that was not matched by the previous case labels. Don’t forget the colon at the end of the default statement (default:).

Investigate

Questions to think about with this program to check your understanding:

Relation question

In the final Console.WriteLine statement, $"Month {month} is in the {season}", why is the word “Month” written in the string but month also appears in braces?

Reveal answer

“Month” in the string is the literal word, “Month”. In contrast, month inside { } refers to a variable — an area in memory holding the name the user entered. As we want the word “Month” followed by the name of the month, we need both in the statement.

Approach question

How could the winter case block be written using an if instead?

Reveal answer

if (month == "December" || month == "January" || month == "February")

Make

Change the program so that:

  1. The function seasons handles the input of the month as a number too. E.g. At the prompt, “Enter the month:”, the user could enter “5” for May instead.

Typical inputs and outputs from the program would be:

Enter the month:
5
Month 5 is in the Spring
🆘 If you're really stuck, use this Parsons code sorting exercise
Complete program
// Seasons program

using System;

class Submission
{
---
    // -------------------------
    // Subprograms
    // -------------------------
    static string seasons(string month)
    {
---
        switch (month)
        {
---
            // Winter months
            case "December":
            case "12":
            case "January":
            case "1":
            case "February":
            case "2":
                return "Winter";
---
            // Spring months
            case "March":
            case "3":
            case "April":
            case "4":
            case "May":
            case "5":
                return "Spring";
---
            // Summer months
            case "June":
            case "6":
            case "July":
            case "7":
            case "August":
            case "8":
                return "Summer";
---
            // Autumn months
            case "September":
            case "9":
            case "October":
            case "10":
            case "November":
            case "11":
                return "Autumn";
---
            // Not a valid month.
            default:
                return "Error";
---
        }
---
    }
---

    // -------------------------
    // Main program
    // -------------------------
    public static void Main(string[] args)
    {
---
        Console.WriteLine("Enter the month:");
        string month = Console.ReadLine();
---
        string season = seasons(month);
---
        Console.WriteLine($"Month {month} is in the {season}");
---
    }
---
}