Switch

Switch statements in computer programming offer a structured and efficient way to handle multiple conditional branches within a program. By evaluating the value of an expression and executing different blocks of code based on its outcome, switch statements streamline decision-making processes and improve code readability. They are particularly useful when dealing with scenarios where a single variable can take on several distinct values, allowing programmers to organize code logic into clear and concise blocks associated with each possible value. Most if not all programming languages will have some form of switch statements and the syntax should be very similar for most languages.

Select Languages

Examples

C

int carCount = 3;

switch (carCount) {
  case 1:
    printf("One car");
    break;
  case 2: 
    printf("Two cars");
    break;
  case 3: 
    printf("Three cars");
    break;
  default: 
    printf("More than three");
}

C#

int carCount = 3;

switch (carCount) {
  case 1:
    Console.WriteLine("One car");
    break;
  case 2: 
    Console.WriteLine("Two cars");
    break;
  case 3: 
    Console.WriteLine("Three cars");
    break;
  default: 
    Console.WriteLine("More than three");
    break;

}

C++

int carCount = 3;

switch (carCount) {
  case 1:
    cout << "One car";
    break;
  case 2: 
    cout << "Two cars";
    break;
  case 3: 
    cout << "Three cars";
    break;
  default: 
    cout << "More than three";
}

Go

import "fmt"

var carCount = 3;

switch carCount {
  case 1:
    fmt.Println("One car")
  case 2: 
    fmt.Println("Two cars")
  case 3: 
    fmt.Println("Three cars")
  default:
    fmt.Println("More than three")
}

Java

int carCount = 3;

switch (carCount) {
  case 1:
    System.out.println("One car");
    break;
  case 2:
    System.out.println("Two cars");
    break;
  case 3: 
    System.out.println("Three cars");
    break;
  default:
    System.out.println("More than three");
}

JavaScript

const carCount = 3;

switch (carCount) {
  case 1:
    console.log('One car');
    break;
  case 2: 
    console.log('Two cars');
    break;
  case 3:
    console.log('Three cars');
    break;
  default:
    console.log('More than three');
}

Kotlin

val carCount = 3;

when (carCount) {
  1 -> print("One car");
  2 -> print("Two cars");
  3 -> print("Three cars");
  else -> {
    print("More than three");
  }
}

MatLab

carCount = 3;

switch carCount
  case 1
    disp('One car');
  case 2
    disp('Two cars');
  case 3
    disp('Three cars');
  otherwise
    disp('More than three');
end

PHP

$carCount = 3;

switch ($carCount) {
  case 1:
    echo "One car";
    break;
  case 2: 
    echo "Two cars";
    break;
  case 3:
    echo "Three cars";
    break;
  default:
    echo "More than three";
}

Python

carCount = 3

match carCount: 
  case 1:
    print("One car")
  case 2:
    print("Two cars")
  case 3:
    print("Three cars")
  case _:
    print("More than three")

R

carCount <- 3

result <- switch(carCount, 
                 "1"="One car", 
                 "2"="Two cars",
                 "3"="Three cars",
                 "More than three")
print(result)

Ruby

$carCount = 3

case $carCount
when 1
  puts "One car"
when 2
  puts "Two cars"
when 3
  puts "Three cars"
else 
  puts "More than three"
end

Rust

let carCount = 3;

match carCount {
  1 => println!("One car"),
  2 => println!("Two cars"),
  3 => println!("Three cars"),
  _ => println!("More than three"),
}

Scala

val carCount: Int = 3;
      
carCount match {
  case 1 => println("One car");
  case 2 => println("Two cars");
  case 3 => println("Three cars");
  case _ => println("More than three");
}
 

Swift

let carCount = 3

switch carCount {
  case 1:
    print("One car");
  case 2: 
    print("Two cars");
  case 3:
    print("Three cars");
  default:
    print("More than three");
}

TypeScript

const carCount:number = 3;

switch (carCount) {
  case 1:
    console.log('One car');
    break;
  case 2: 
    console.log('Two cars');
    break;
  case 3:
    console.log('Three cars');
    break;
  default:
    console.log('More than three');
}

Copyright 2025. All Rights Reserved. IronCodeMan.