top of page

Switch and Case

A switch statement is similar to an if statement in the sense that it will check to see if a condition is met then act appropriately from there. It is different, however, in the sense that they first define something which will be checked then define cases upon which they will be checked against.

The syntax for switch statements are fairly unique compared to other C functions and can be written as follows:

switchandcase1.png

Although if statements may be used to perform the same tasks it is often recommended to instead use switch statements while working with more than a few parameters for readability and efficiencies sake. This is because larger switch statements are accessed using lists in which each parameter can be simultaneously checked against instead of checking one by one.

Example:

Output:

switchandcase3.png
switchandcase2.png

The default case can be used to define what actions should be taken if none of the other parameters are met. default is usually placed at the end after all the other cases but this is not required.

Let's add the default parameter below case 1 and see what happens.

Example:

Output:

switchandcase5.png
switchandcase4.png
  • Twitter
  • LinkedIn
  • discord-logo--v2
  • kisspng-github-pages-logo-repository-fork-github-logo-1-magentys-5b69de71b51265

Dragon Eye Intelligence LLC

bottom of page