0
0
Swiftprogramming~15 mins

Switch statement power in Swift - Mini Project: Build & Apply

Choose your learning style9 modes available
Switch statement power
📖 Scenario: You are creating a simple program to identify the type of a day based on its name. This helps you plan your activities better.
🎯 Goal: Build a Swift program that uses a switch statement to print the type of day (weekday or weekend) for a given day name.
📋 What You'll Learn
Create a variable called day with the exact value "Saturday".
Create a variable called dayType to hold the result.
Use a switch statement on day with cases for weekdays and weekends.
Assign "Weekend" or "Weekday" to dayType inside the switch.
Print the value of dayType.
💡 Why This Matters
🌍 Real World
Switch statements help you make decisions based on different values, like choosing actions for days of the week in scheduling apps.
💼 Career
Understanding switch statements is important for writing clear and efficient code in many programming jobs, especially when handling multiple conditions.
Progress0 / 4 steps
1
DATA SETUP: Create the day variable
Create a variable called day and set it to the string "Saturday".
Swift
Need a hint?

Use var day = "Saturday" to create the variable.

2
CONFIGURATION: Create the dayType variable
Create a variable called dayType and set it to an empty string "".
Swift
Need a hint?

Use var dayType = "" to create the variable.

3
CORE LOGIC: Use switch statement to set dayType
Use a switch statement on the variable day. For cases "Monday", "Tuesday", "Wednesday", "Thursday", and "Friday", set dayType to "Weekday". For cases "Saturday" and "Sunday", set dayType to "Weekend". Use a default case to set dayType to "Unknown".
Swift
Need a hint?

Use commas to group multiple cases in one line inside the switch.

4
OUTPUT: Print the dayType
Write a print statement to display the value of the variable dayType.
Swift
Need a hint?

Use print(dayType) to show the result.