switch/match construct
C
C++
C#
Common Lisp
Dart
F#
Go
Java
JavaScript
Kotlin
Python
Rust
C
C
int x = 2;
switch(x)
{
case 1:
printf("x = 1 \n");
break;
case 2:
printf("x = 2 \n");
break;
case 3:
printf("x = 3 \n");
break;
default:
printf("x is undefined \n");
break;
}
C++
C++
#include <iostream>
int main()
{
int x = 2;
switch(x)
{
case 1:
std::cout << "x = 1" << std::endl;
break;
case 2:
std::cout << "x = 2" << std::endl;
break;
case 3:
std::cout << "x = 3" << std::endl;
break;
default:
std::cout << "x is undefined" << std::endl;
break;
}
// switch-case with variable initialization (C++17)
char op = '+';
int n = 10;
switch(int k{2}; op)
{
case '+':
std::cout << n + k << std::endl;
break;
case '-':
std::cout << n - k << std::endl;
break;
case '*':
std::cout << n * k << std::endl;
break;
}
}
C#
C#
int x = 2;
switch(x)
{
case 1:
Console.WriteLine("x = 1");
break;
case 2:
Console.WriteLine("x = 2");
break;
case 3:
Console.WriteLine("x = 3");
break;
default:
Console.WriteLine("x is undefined");
break;
}
Returning from switch (Switch Expression):
C#
int x = 2;
int result = x switch {
1 => 1,
2 => 4,
3 => 9,
_ => -1
};
Console.WriteLine(result); // 4
Common Lisp
Lisp
(let ((x 2))
(cond
((eql x 1) (format t "x=1~%"))
((eql x 2) (format t "x=2~%"))
((eql x 3) (format t "x=3~%"))
( t (format t "x is undefined~%"))))
Dart
Dart
int x = 2;
switch(x){
case 1:
print("x = 1");
case 2:
print("x = 2");
case 3:
print("x = 3");
default:
print("x is undefined");
}
F#
F#
let x = 2
match x with
| 1 -> printfn "x = 1"
| 2 -> printfn "x = 2"
| 3 -> printfn "x = 3"
| _ -> printfn "x is undefined"
Returning from the match:
F#
let x = 2
let result = match x with
| 1 -> 1
| 2 -> 4
| 3 -> 9
| _ -> -1
printfn "%d" result // 4
Go
Go
package main
import "fmt"
func main() {
x := 2
switch(x){
case 1:
fmt.Println("x = 1")
case 2:
fmt.Println("x = 2")
case 3:
fmt.Println("x = 3")
default:
fmt.Println("x is undefined")
}
}
Java
Java
class Program {
public static void main(String[] args) {
int x = 2;
switch(x) {
case 1:
System.out.println("x = 1");
break;
case 2:
System.out.println("x = 2");
break;
case 3:
System.out.println("x = 3");
break;
default:
System.out.println("x is undefined");
break;
}
}
}
Returning from switch (Switch Expressions):
Java
int x = 2;
int result = switch(x) {
case 1 -> 1;
case 2 -> 4;
case 3 -> 9;
default -> -1;
};
System.out.println(result); // 4
JavaScript
JavaScript
const x = 2;
switch(x)
{
case 1:
console.log("x = 1");
break;
case 2:
console.log("x = 2");
break;
case 3:
console.log("x = 3");
break;
default:
console.log("x is undefined");
break;
}
Kotlin
Kotlin
val x = 2
when (x) {
1 -> println("x = 1")
2 -> println("x = 2")
3 -> println("x = 3")
else -> println("x is undefined")
}
Return from when:
Kotlin
val x = 2
val result = when (x) {
1 -> 1
2 -> 4
3 -> 9
else -> -1
}
println(result)
Python
Python
x = 2
match x:
case 1:
print("x = 1")
case 2:
print("x = 2")
case 3:
print("x = 3")
case _:
print("x is undefined")
Rust
Rust
fn main(){
let x = 2;
match x {
1 => println!("x = 1"),
2 => println!("x = 2"),
3 => println!("x = 3"),
_ => println!("x is undefined")
}
}
Returning from the match:
Rust
fn main(){
let x = 2;
let result = match x {
1 => 1,
2 => 4,
3 => 9,
_ => -1
};
println!("{}", result);