🖥️
Intro to Computer Science (ICS3U/C)
  • An Introduction to Computer Science
  • Videos & Slides
  • Unit 1: In the Beginning
    • The History of Computers
    • Binary & Logic
      • Bits and Bytes (Binary)
      • Transistors (Changing Bits)
      • Logic Gates
        • Poster
        • Logic.ly
    • The Parts of a Computer
  • Unit 2: Intro to Code
    • How Do We Code?
      • Coding Conventions (Rules)
      • Commenting Code
    • What is HTML?
      • Hello World! (in HTML)
      • HTML Slideshow
    • Hello World!
    • Input / Output
      • The Console
      • Prompt, Alert, Confirm
    • Variables & Data
      • Strings (Text)
      • Numbers (Values)
        • Converting & Rounding
        • The Math Object
          • Random Numbers
      • Booleans
        • Truthiness
      • Arrays
  • Unit 3: Control Flow
    • Conditionals (if this, do that)
      • If...Else
        • Logical Operators
      • Switch / Case
      • Ternary Operators
    • Loops (Repeating Code)
      • For...Loop
      • While & Do/While Loops
    • Debugging
  • Unit 4: Functions
    • Functional Programming
    • User Defined Functions
      • Hoisting and Scope
    • Calling a JS Function
  • TL;DR
    • Programming Basics
    • Slideshows & Demos
    • Javascript Syntax Poster
  • Advanced Topics
    • Recursion
    • Structures & Algorithms
    • Mmm... Pi
  • External Links
    • Typing Club!
    • repl.it
    • Khan Academy
    • Geek Reading
    • ECOO CS Contest
Powered by GitBook
On this page
  • Switch Syntax
  • Example 1 - Switch on Number
  • Example 2 - On Text
  • Final Thoughts
  1. Unit 3: Control Flow
  2. Conditionals (if this, do that)

Switch / Case

PreviousLogical OperatorsNextLoops (Repeating Code)

Last updated 6 years ago

Use a Switch or Case statement if you are going to have many else if statements or the condition is not inherently boolean in nature (like the value of a string, for example).

Switch Syntax

Same syntax for C++, Java, Javascript, and many other languages.

switch(expression) {
   case value :
      // Code to run
      break; // optional to break out of the switch block
   
   case value :
      // Code to run
      break; // optional to break out of the switch block
   
   /** You can have any number of case statements. **/
   
   default : // Optional case to catch any other possibilities
      // Code to run
}

Python does not have a Switch / Case implementation. Instead, you use something called a dictionary and call it a switcher. You can read more about that on your own:

An example of switching (in this example, we are switching on a user's input):

// Javascript
let userInput = prompt("Please enter a colour", "blue");

switch (userInput) {
   case "red":
      // code for what happens when user enters red
      break;
   case "blue":
      // code for what happens when user enters blue
      break;
   case "black":
      // code for what happens when user enters black
      break;
   case "yellow":
      // code for what happens when user enters yellow
      break;
   default:
      // code for what happens when user enters anything else
}

Rather than reinventing the wheel, here are some other sites with great content on this topic:

Example 1 - Switch on Number

Example 2 - On Text

Final Thoughts

The switch statement is not used on boolean values. It is strictly meant for multiple options similar to a menu or list. General rule of thumb - when your code is a bunch of if...else if statements, it might be better suited for a switch statement unless you are using boolean operators (&& and ||).

(this one is in the C language but the concept is the same)

The following example creates a random number from 1-10 (inclusive) and decides what to do based on that number. The random number is just a way of faking user input or a value from some other function. The example does something specific for numbers 1, 2, 3, 7, and 9 but all the rest fall under the default case. You will need to scroll down in the example or due to the length.

This example switches on text instead of a number. Same procedure as numbers, just a string instead. You might need to scroll the example or to see it better.

Javascript.info
Tutorials Teacher
Programiz Learn C
open it in a new window
open it in a new window
How to implement a switch-case statement in Python - JAXenterJAXenter
LogoSign up to continue codingreplit