Skip to content
Justice Tech School

switch

switch compares an integral value with cases. Use break to stop fall through.

Example
#include <iostream>

int main() {
  int d = 2;
  switch (d) {
    case 1: std::cout << "one\n"; break;
    case 2: std::cout << "two\n"; break;
    default: std::cout << "other\n";
  }
  return 0;
}

Result

two

Exercise

Stop fall through.

case 1: f(); ;

Quiz Test

Can switch use strings?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.