What does "case" mean? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What does "case" mean?

13th Jun 2016, 6:10 PM
Ryan
Ryan - avatar
2 Answers
0
"Case" means a potential situation, something that could happen. It is followed by what command to execute if that situation does happen. Example: \\ this code will print what card was picked from a deck of cards, according to the value stored by the variable card. var card = 1 switch card { case 1: print("You picked an Ace") case 2..10: print("You picked /(card)") case 11: print("You picked a Jack") case 12: print("You picked a Queen") case 13: print("You picked a King") default: print("Error: Card number is invalid.") \\ see? Now, in CASE the variable card is equal to 1, it will print the Ace message. In case the variable is equal to a number from 2 to 10, it will print a message saying what number it is. In case it's 11, 12 or 13, it will print who it is (either Jack, Queen or King). Finally, the "default" allows us to print an error message if the variable somehow equals to something under 1 or above 13. Did that help? :)
27th Jun 2016, 2:01 PM
Samuel
0
Oops, I made a few mistakes with the slashes though. The comment slashes should be forward, and there should be a backslash instead of a forward-slash in this part of my example: case 2..10 print("You picked /(card)") Therefore, it should be : case 2..10 print("You picked \(card)") See the difference?
27th Jun 2016, 2:07 PM
Samuel