Thursday 11 January 2024

Switch Case

let score = 85

switch score {

case 0..<60:

    print("You failed.")

case 60..<70:

    print("You passed, but it could be better.")

case 70..<80:

    print("You did well.")

case 80..<90:

    print("You did very well.")

case 90...100:

    print("Congratulations! You aced it.")

default:

    print("Invalid score.")

}

Example 2


var aNumber = Int(readLine()!)!

func dayOfTheWeek(day: Int) {
    // Use the provided parameter 'day' in the switch statement
    switch day {
    case 1:
        print("Monday")
    case 2:
        print("Tuesday")
    case 3:
        print("Wednesday")
    case 4:
        print("Thursday")
    case 5:
        print("Friday")
    case 6:
        print("Saturday")
    case 7:
        print("Sunday")
    default:
        print("Error: Invalid day")
    }
}

// Call the function with the provided variable 'aNumber'
dayOfTheWeek(day: aNumber)

No comments:

Post a Comment

How to connect to VPS with Private Key on Mac OS

1. Use the cd command to navigate to the directory where your private key file is stored. For example, if your private key is in the Downlo...