0
0
Goprogramming~15 mins

Switch statement in Go - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Switch Statement in Go
๐Ÿ“– Scenario: You are creating a simple program that tells the type of day based on a number input. This is like checking the day of the week where 1 means Monday, 2 means Tuesday, and so on.
๐ŸŽฏ Goal: Build a Go program that uses a switch statement to print the name of the day for numbers 1 to 7. If the number is not between 1 and 7, print "Invalid day".
๐Ÿ“‹ What You'll Learn
Create a variable dayNumber with an integer value.
Create a switch statement using dayNumber.
Use case blocks for numbers 1 to 7 to print the correct day name.
Use default case to print "Invalid day" for other numbers.
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Switch statements are used in many programs to choose actions based on different values, like menu selections or day names.
๐Ÿ’ผ Career
Understanding switch statements helps in writing clear and efficient code for decision making in software development jobs.
Progress0 / 4 steps
1
Create the dayNumber variable
Create an integer variable called dayNumber and set it to 3.
Go
Need a hint?

Use := to declare and assign the variable in one line.

2
Start the switch statement
Add a switch statement using the variable dayNumber inside the main function.
Go
Need a hint?

Write switch dayNumber { to start the switch block.

3
Add cases for days 1 to 7 and default
Inside the switch dayNumber, add case blocks for numbers 1 to 7. Use fmt.Println to print the day names: 1 - Monday, 2 - Tuesday, 3 - Wednesday, 4 - Thursday, 5 - Friday, 6 - Saturday, 7 - Sunday. Add a default case to print "Invalid day".
Go
Need a hint?

Each case ends with a colon and the code to run is indented below it.

4
Print the result
Run the program to print the day name for dayNumber. The output should be Wednesday.
Go
Need a hint?

Just run the program. It will print the day name automatically.