switch/match construct
C
C++
Common Lisp
Dart
F#
Go
Java
JavaScript
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);
Explore More IT Terms
#
A
- A Guide to SQL Query Formatting
- A/B testing
- Agile
- Algorithm complexity in 5 minutes
- Algorithms and Data Structures in C#
- An overview of the C # programming language
- An overview of the Python programming language
- Anaconda Python
- Android
- Android App Bundle
- Android SDK
- Angular
- Ansible
- Apache
- Apache Airflow
- Apache Kafka
- Apache Tomcat
- App Store
- AppCode
- Array-based stack
- ArrayList
- ASCII
- ASP.NET
- Assembly Language Lessons
B
C
D
- Data Analytics: applications of data analysis in companies
- Data Engineer - Who is it, what does a data engineer do, and an overview of the profession
- Data modeling: what it is, types, and process steps.
- Data preprocessing: a complete guide for beginners and professionals.
- Data structure
- Data structures
- Defining Aliases
- Defining Arrays
- Deque
- Developing a Website from Scratch
- Digital data: understand the importance of this asset for businesses.
- Doubly linked lists
E
F
H
- Handling errors and exceptions
- How to effectively organize your workflow
- How to Learn Java: Tips for Beginner Developers
- How to Learn PHP: A Beginner's Guide
- How to Use S3 Storage in Kubernetes with CSI
- HTML
- HTML and CSS: Definition, Application, and Operating Principles
- HTML and CSS. Layout from Scratch: What to Learn, Where to Learn, and How Long Will It Take?
- HTML Frame Structure
- HTML Link Formatting
I
- if..else construction
- Inheritance in Java: A Complete Guide to Principles and Implementation
- Inserting an Image
- Interactive Python Tutorial – Learn Programming from Scratch
- Interview Problem: Finding a Deleted Element in O(N)
- Interview Scare: The FizzBuzz Challenge
- Introduction to C++
- Introduction to Machine Learning
- Introduction to programming languages
- IT Specialist Resume (CV)
J
K
M
O
P
S
- SFML Graphics Library Tutorials
- SQL commands: see what they are, what the main ones are + examples
- SQL Interview Questions and Tasks
- SQL Lessons
- SQL Stored Procedures
- SQL Syntactic Sugar: The COALESCE Function
- Stack
- Start in analytics: Python or R
- Statistical analysis: importance for decision making.
- String formatting in Python
- Swift Lessons
- switch/match construct
T
W
- What are databases, and why do they need DBMS and SQL?
- What do Linux distributions consist of?
- What is .NET and what is it used for?
- What is a GPU in a computer, in simple terms?
- What is Big Data? Introduction, Types, Characteristics, and Examples
- What is Golang and what is it used for?
- What is Haskell and what is it used for?
- What is Kotlin and what is it used for?
- What is Linux? The History of Linux
- What is machine learning, and how does it work?
- What is Power BI: everything about the data analytics software
- What is the C++ programming language?
- What is the OSI Model: A Complete Explanation of the Seven Layers and Their Role in Networking
- Where to start learning the C programming language?
- Which Linux distribution should you choose? A Linux distribution overview
