Day of the Week Using Switch

Day of the Week Using Switch


Introduction

In programming, we often need to make decisions based on a specific value. The switch statement in C# is a powerful tool for this purpose—it allows your program to choose from multiple options depending on the value of a variable. In this lesson, you’ll learn how to use a switch statement to determine and display the day of the week based on user input.

This concept is a practical example of decision-making in programming. Instead of writing multiple if-else statements, the switch statement helps make your code cleaner, more organized, and easier to read. By the end of this lesson, you’ll understand how to use switch statements to handle multiple conditions efficiently.


Objectives

Before coding, let’s set our learning goals. This lesson focuses on developing logical decision-making through the use of the switch statement in C#. The objectives are divided into four Outcome-Based Education (OBE) domains:

  • Understand: Explain the structure and function of the switch statement in C#.
  • Learn: Identify the correct syntax and use cases for the switch statement.
  • Practice: Write and execute a C# console program that displays the day of the week using a switch structure.
  • Apply: Modify and extend the code to handle invalid inputs or additional features such as displaying special messages for weekends.

Through these objectives, you’ll strengthen your skills in structured decision-making, helping you write cleaner and more efficient code.


Beginner-Friendly Source Code and Step-by-Step Instructions

Step-by-Step Instructions

  1. Open Visual Studio Code
    Ensure that the .NET SDK is installed. To check, type the following in your terminal:

    dotnet --version
    
  2. Create a new project folder
    Example: D:\2025-1_doe_bsis_1a
  3. Open the folder in VS Code
    Go to File → Open Folder…
  4. Create a new console project
    In the terminal, type:

    dotnet new console -n day_of_week_switch
    
  5. Navigate to the project folder
    cd day_of_week_switch
    
  6. Open Program.cs and replace its contents with the following code.

C# Program: Day of the Week Using Switch

using System;

namespace DayOfWeekApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a number (1-7) to get the day of the week: ");
            int dayNumber = Convert.ToInt32(Console.ReadLine());

            switch (dayNumber)
            {
                case 1:
                    Console.WriteLine("Monday");
                    break;
                case 2:
                    Console.WriteLine("Tuesday");
                    break;
                case 3:
                    Console.WriteLine("Wednesday");
                    break;
                case 4:
                    Console.WriteLine("Thursday");
                    break;
                case 5:
                    Console.WriteLine("Friday");
                    break;
                case 6:
                    Console.WriteLine("Saturday");
                    break;
                case 7:
                    Console.WriteLine("Sunday");
                    break;
                default:
                    Console.WriteLine("Invalid input! Please enter a number between 1 and 7.");
                    break;
            }
        }
    }
}

How to Run the Program

  • Open the Terminal inside VS Code.
  • Type the command below:
    dotnet run
    

Example Output

Example 1:

Enter a number (1-7) to get the day of the week: 3
Wednesday

Example 2:

Enter a number (1-7) to get the day of the week: 6
Saturday

Example 3 (Invalid Input):

Enter a number (1-7) to get the day of the week: 9
Invalid input! Please enter a number between 1 and 7.

Explanation of the Program

  1. The program begins by asking the user to input a number from 1 to 7.
  2. The input is read as a string and converted to an integer using Convert.ToInt32().
  3. The switch statement evaluates the variable dayNumber.
  4. Each case corresponds to a specific day of the week, from Monday to Sunday.
  5. When a matching case is found, the corresponding message is printed, and the break statement exits the switch block.
  6. If the user enters a number outside the range 1–7, the default case executes, displaying an error message.

This structure is ideal for situations where multiple discrete values need different outputs. It keeps the program organized, efficient, and easy to understand.


Summary

In this lesson, you learned how to use the switch statement in C# to display the day of the week based on user input. You explored how switch provides a cleaner alternative to multiple if-else statements. By understanding how each case works and how to use the default clause, you can now apply this logic to other programs that require handling multiple specific values. Mastering the switch structure is a vital step toward writing professional, readable, and efficient C# programs.


Quiz (5 Items – Bloom’s Taxonomy)

  1. What is the main purpose of a switch statement?
    A. To handle repetitive loops
    B. To make decisions based on multiple possible values
    C. To create arrays
    D. To convert data types
  2. What keyword is used to end each case in a switch statement?
    A. stop
    B. end
    C. break
    D. continue
  3. Which statement handles inputs that do not match any case?
    A. if
    B. else
    C. default
    D. return
  4. What will happen if you forget the break statement in a case block?
    A. The program will stop immediately.
    B. The next case will also run (fall-through).
    C. It will show an error.
    D. Nothing will happen.
  5. What is the output if the user enters the number 1?
    A. Sunday
    B. Monday
    C. Tuesday
    D. Invalid input

Exercises, Assessment, and Lab Exam

Introduction to Exercises

These activities are designed to help you strengthen your understanding of the switch statement by applying it to real-world scenarios. You’ll get to experiment with multiple inputs, customize outputs, and develop your problem-solving skills.

Exercises

  1. Modify the program to display messages such as “Weekday” or “Weekend.”
  2. Create a version that uses string input (e.g., “Mon”, “Tue”) instead of numbers.
  3. Ask the user to input a month number (1–12) and display the month name.
  4. Enhance the program to handle both uppercase and lowercase input using ToLower().
  5. Add a feature that displays motivational quotes based on the day of the week.

Assessment

  • Quiz: Answer the 5-item multiple-choice test above.
  • Performance Task: Write a C# program that displays the corresponding month name based on the user’s numeric input (1–12).

Lab Exam

Create a program that accepts a number (1–5) and displays:

  • 1 → “Excellent”
  • 2 → “Very Good”
  • 3 → “Good”
  • 4 → “Fair”
  • 5 → “Poor”
    Use the switch statement and include a default case for invalid inputs.

You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive free capstone projects resources and computer programming tutorials.

Hire our team to do the project.

, , , , , , , , ,

Post navigation