Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Basics (retired) Control Flow Switch Statement

confused

what does the default statements purpose?

1 Answer

Greg Kaleka
Greg Kaleka
39,019 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.