Use of switch.. case

Wolframore

Joined Jan 21, 2019
2,609
That’s very cool thanks for sharing. Hey dj I just started using switchcase in my most recent code do you know if there are significant differences to using if and else? I wonder if it uses less memory?
 

Papabravo

Joined Feb 24, 2006
21,157
That’s very cool thanks for sharing. Hey dj I just started using switchcase in my most recent code do you know if there are significant differences to using if and else? I wonder if it uses less memory?
Depends entirely on the compiler and the machine hardware that may support multi-way branching.
 

Thread Starter

djsfantasi

Joined Apr 11, 2010
9,156
I agree with Papabravo. I use the construct because I find the resulting code much easier to read and maintain.

You can code the same functionality with if statements, but it quickly becomes confusing. Do you use if...then...else or standalone if statements? Code blocks for each different condition are more difficult to manage...

So, that’s when you use switch...case
 

Wolframore

Joined Jan 21, 2019
2,609
It seems like it uses less memory, I've just received 1K chips and using port manipulation and case switch I'm able to make code to control two channel using random delays with less than half the memory. It is a very limited chip but they cost about $0.25 each and that alone is amazing when you need more simple tasks...

For the more complex codes I use functions and control them using if statements... having them encapsulated in subroutines... but I like learning new tricks...
 

Papabravo

Joined Feb 24, 2006
21,157
For those who like to dig into the internals of things it might be instructive to see how code is generated by a compiler for a switch...case statement and any of the alternatives. All you have to do is get the compiler to produce the assembly language source listing of the compiled code.
 
Top