Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Basics (retired) Control Flow Switch Statement

confused

what does the default statements purpose?

1 Answer

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Hunter,

The default case will be triggered if none of the other cases are. For example, say you had a game app, and if someone scored an 8, they got a bronze medal, if they scored a 9, they got a silver medal, and a 10 got a gold medal. Anything below an 8 got a "better luck next time" message. The easiest way to do this would be a switch statement.

switch(score){
    case 8:
        message = "Bronze Medal!"
    case 9:
        message = "Silver Medal!"
    case 10:
        message = "Gold Medal!"
    default:
        message = "Better luck next time!"
}

The default case makes it so we don't have to write out every possible case. It also protects us in case something weird happens and an unforeseen case happens.