SSi | Dégceat
Switch Case :
The ‘switch case’ statement is like the ‘if... else’ statement but a cleaner and
quicker way than ‘if... else’. It is present in languages like C++ or Java.
We use switch case specifically, when we need to run only a specific code block,
and if the other code blocks do not satisfy the condition, they will be skipped. If
this code block is checked manually, the complexity of code increases making it
unfavorable.
Using a switch case statement, one can test a variable for being one of the many
possible values. The variable will follow the code for the particular value it takes.
The significance of the switch case statement includes-
Easy debugging
Easy to read and understand
Easy to maintain
Easy to verify the values to be checked and handled
Syntax:
match term:
case pattern-1:
action-1
case pattern-2:
action-2
case pattern-3:
action-3
case_:
action-default
Note that the underscore symbol is what you use to define a default case for the
switch statement in Python.
An example of a switch statement written with the match case syntax is shown
below. It is a program that prints what you can become when you learn various
programming languages:SSi | Dégccat
lang = input("What's the programming language you want to learn? ")
match lang:
case "JavaScript":
print("You can become a web developer.)
case "Python":
print("You can become a Data Scientist")
case "PHP":
print("You can become a backend developer")
case "Solidity":
print("You can become a Block chain developer")
case "Java":
print("You can become a mobile app developer")
case _:
print("The language doesn't matter, what matters is solving problems.")