0
0
C++programming~15 mins

Switch statement in C++ - Mini Project: Build & Apply

Choose your learning style9 modes available
Using a Switch Statement in C++
📖 Scenario: You are creating a simple program that tells the name of a day based on a number input from 1 to 7. Each number corresponds to a day of the week.
🎯 Goal: Build a program that uses a switch statement to print the correct day name for a given number.
📋 What You'll Learn
Create an integer variable called dayNumber with a value from 1 to 7
Create a switch statement that checks dayNumber
Use case labels for numbers 1 through 7, each printing the correct day name
Include a default case that prints "Invalid day number"
Print the day name or the invalid message as the final output
💡 Why This Matters
🌍 Real World
Switch statements are used in many programs to choose actions based on a value, like menu selections or commands.
💼 Career
Understanding switch statements helps in writing clear and efficient code for decision-making in software development.
Progress0 / 4 steps
1
Create the day number variable
Create an integer variable called dayNumber and set it to 3.
C++
Need a hint?

Use int dayNumber = 3; to create the variable.

2
Start the switch statement
Write a switch statement that checks the variable dayNumber.
C++
Need a hint?

Use switch(dayNumber) { } to start the switch.

3
Add cases for days 1 to 7
Inside the switch(dayNumber), add case labels for numbers 1 to 7. For each case, write a cout << "Monday" << endl; statement to print the day name (e.g., "Monday" for 1). Add a break; after each print statement. Also add a default case that prints "Invalid day number". Make sure to include #include <iostream> and using namespace std; later.
C++
Need a hint?

Each case should print the correct day with cout << "Day" << endl; and end with break;. The default prints the invalid message.

4
Run the program to see the output
Add the necessary includes, namespace, and main function to run the program. The output should be Wednesday.
C++
Need a hint?

Ensure #include <iostream> and using namespace std; are at the top, and return 0; at the end of main().