4 ms·
Actually in the hardware world I see switch(true) a lot. It is very useful for creating a priority encoder. For example you have 3 interrupts with priorities in
by tonmoy 9y ago
Actually in the hardware world I see switch(true) a lot. It is very useful for creating a priority encoder. For example you have 3 interrupts with priorities int0 > int1 > int2. Then this code is pretty clean and concise:
Switch(true) {
Case(int0):
// Do stuff for interrupt 0
Case(int1):
// Do stuff for interrupt 1
...
- meowface 9y agoI stand corrected. Assuming no fallthrough, what are the advantages of this over chaining if and else-if? Just to look cleaner?
- consz 9y agoI believe the point is to explicitly take advantage of the fallthrough.
- meowface 9y agoWell, then it would just be chaining if statements.
- tonmoy 9y agoNot much, but I’d say it is more maintainable as well (you can change priority/get rid of the top one without affecting the following lines)