Press ESC to close

Swift Switch Case

One of the most important features in programming languages is decision making. Switch Case is the most used in decision making. Java and C # are different again according to Swift. We have also mentioned the decision of İf Else, which is a spontaneous decision structure in our previous courses. In this article, I will describe the switch case structure on a sample application. In this scenario we will get a random number. We will then print the spelling of this number on the screen. you can find each topic in this example application in the previous postings.

First we must create a random number. You can see how I created random numbers in the previous article. We should be careful, the random number should be in a certain range so that our work is easier. I’ll get the numbers up to 10. Then we write a switch to write the name of the variable to be executed on which variable. Then, with the case options, we predict what values this variable will take and write what happens when it gets those values.

let rastgeleSayi = arc4random_uniform(10)
        switch rastgeleSayi {
        case 0:
            label1.text="Sıfır"
            break
        case 1 :
            label1.text="Bir"
            break
        case 2 :
            label1.text="İki"
            break
        case 3 :
            label1.text="Üç"
            break
        case 4 :
            label1.text="Dört"
            break
        case 5:
            label1.text="Beş"
            break
        case 6:
            label1.text = "Altı"
            break
        case 7:
            label1.text="Yedi"
            break
        case 8 :
            label1.text="Sekiz"
            break
        case 9 :
            label1.text="Dokuz"
            break
        default:
            break
        }

I am using the random number variable that I created in this example, and I print this screen as the number which comes up. The simplest and most useful way to do this with the if-else construct is to use a switch-case construct that would be unnecessarily long and absurd.

If you have any questions that you have in mind, you can reach me by email or comment.

Leave a Reply

Your email address will not be published. Required fields are marked *