what's more convenient in control structure is it if-else or switch ??? or both are convenient ?? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

what's more convenient in control structure is it if-else or switch ??? or both are convenient ??

5th Mar 2021, 12:50 PM
ashii
2 Respostas
+ 3
both: depends on use cases... 'switch' allows to avoid lot of 'else if', if conditions are just to check a variable against many values ^^
5th Mar 2021, 12:56 PM
visph
visph - avatar
+ 3
Use `switch` when you validate a value against one or more finite values. C++ does not support chained expressions evaluation or range of values evaluation in `switch` cases (some compiler provides it as extension e.g. gcc - http://gcc.gnu.org/onlinedocs/gcc/Case-Ranges.html) Use `if..else if..else` when you need to validate against range of values, when there is need to evaluate chained expressions, or when you need to use the benefits of short circuit evaluation - https://softwareengineering.stackexchange.com/questions/325009/short-circuit-evaluation-is-c I personally think it's not a matter of convenience, it's a matter of choosing one that was meant for the purpose.
5th Mar 2021, 1:11 PM
Ipang