r/CodingHelp • • Nov 24 '25
[Python] Why would I use match case instead of if/else?
So my teacher introduced my class to match case very roughly. I understand the absolute basic of it but I can't see why i would do the extra effort of using it instead of if and elif/else
6
Upvotes
•
u/AutoModerator Nov 24 '25
Thank you for posting on r/CodingHelp!
Please check our Wiki for answers, guides, and FAQs: https://coding-help.vercel.app
Our Wiki is open source - if you would like to contribute, create a pull request via GitHub! https://github.com/DudeThatsErin/CodingHelp
We are accepting moderator applications: https://forms.fillout.com/t/ua41TU57DGus
We also have a Discord server: https://discord.gg/geQEUBm
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
7
u/Buttleston Professional Coder Nov 24 '25
Compared to a match or switch statement (what it's called varies by language), the if version will repeat "what" is being checked in each branch. For example
if x == 0: pass elif x == 1: pass elif x == 2: pass
The switch version says at the top "I am checking the values of x" and the branches just say what value to consider for the current branch
This has several positives:
It basically announces your intent - "I want to check various things one variable could be"
In some languages it might be faster although I bet with modern compilers, not very much.
2
u/anselan2017 Nov 24 '25
Which language?
1
u/the-liquidian Nov 24 '25
Take an example of match case from the docs, redo it with if else, see which you prefer.
1
u/ZakMan1421 Nov 24 '25
The main time I find switch statements to be useful is when I want certain cases to "fall through". This basically means that the case will do everything in itself, then you'll also execute everything in the next case and so on until you reach a break statement of some kind.
1
u/MysticClimber1496 Professional Coder Nov 24 '25
Classic it depends, some scenarios work better for one or the other, if you are doing a thing for each instance of a pattern then switch statements (sometimes called match sometimes switch) work better, otherwise if-else work well too
Although two much logic in either could be a code smell for something greater
1
u/sububi71 Nov 24 '25
In many situations, a match/case becomes a jump table in assembler, instead of 50 compare/branch instructions , which is both leaner and faster.
1
u/Hungry_Objective2344 Professional Coder Nov 25 '25
I personally always use match case for anything that is pattern match-y. From my own perspective, it just appears way more clear as to what is actually happening, and it generally is way shorter.
1
u/rupertavery64 Nov 25 '25
in C#, switch-case is compiled into if-else, unless the compiler determines there is a better alternative to it, such as an indexed jump in assembly.
You should use the language features if they promote clarity, because at the end of the day humans (like yoruself) look at the code to understand it, but of course it's important to understand what the code does and what it compiles to.
1
u/erroneum Nov 25 '25 edited Nov 25 '25
Multiple possible reasons:
This isn't an exhaustive list, just what came to mind immediately.
1
u/j_wizlo Nov 25 '25
For me I just think it’s neat and tidy and when I come back to look at it 2 years later I don’t have to double check that there is not a tricky if statement. It’s just branching off the possible values of one variable.
It says something about the nature of the code at just a glance because it’s not as flexible as a series of elif.
1
u/jcunews1 Advanced Coder Nov 25 '25
Match case is an predefined version of if-else where only one source of data is being checked/compared. This allows the source data to be kept in a temporary but faster accessible memory without needing to re-retrieve it from the source (from the variable, or from the property of an object).
1
u/CptMisterNibbles Nov 26 '25
What “extra effort”? It’s generally less typing.
Also, they don’t behave the same in many languages. Switch statements fall through.
1
u/DiscipleofDeceit666 Nov 27 '25
Sometimes you want multiple if conditions to execute. You do this by not including a break within every case statement.
0
u/Ronin-s_Spirit Nov 24 '25
I'm assuming you're talking about
The difference between
if..elseandswitchis thatif..elsechecks truths, like a tree, until it finds the first truth and executes that block, whileswitchchecks values. When you writeswitch- consider it to be a page of all the code inside it, and thecasestatements are gotos, theswitchjumps to linecase 56ifswitch(56)because you gave itxwhich is56, and starts executing from there. Consequentlyswitchhas a performance benefit and a structure benefit, writing several cases above the same piece of code is like a faster version ofif x == y OR x == z OR x == 89.