+ 1

In what ways will a switch will be worthy as a alternate to nested if ??.

Is switch a real replacement to nested if ?.

14th Jun 2019, 7:22 PM
Afzal Ali
Afzal Ali - avatar
8 Respostas
+ 17
There are three important things to know about the switch statement: • The switch differs from the if in that switch can only test for equality, whereas if can evaluate any type of relational or logical expression. • No two case constants in the same switch can have identical values. Of course, a switch statement enclosed by an outer switch may have case constants that are the same. • If character constants are used in the switch statement, they are automatically converted to integers.
14th Jun 2019, 8:10 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 3
Switch may be more readable, rather than having a really large if-else-chain with just equality checks, but in my opinion, switch isn't that important, you'll be fine using if statements
14th Jun 2019, 7:26 PM
Airree
Airree - avatar
+ 3
No, it isn't. Although it has a really awkward syntax
14th Jun 2019, 7:38 PM
Airree
Airree - avatar
+ 2
No, switch can only test if a value equals to something, it can't do comparison.
14th Jun 2019, 7:36 PM
Airree
Airree - avatar
+ 1
Is that possible to implement the following code using switch #include <stdio.h> int main() { int score = 89; if (score >= 90) printf("%s", "Top 10% \n"); else if (score >= 80) printf("%s", "Top 20% \n"); else if (score > 75) printf("%s", "You passed.\n"); else printf("%s", "You did not pass.\n"); }
14th Jun 2019, 7:32 PM
Afzal Ali
Afzal Ali - avatar
+ 1
So... switch is aint a TOTAL replacement to if else if.
14th Jun 2019, 7:37 PM
Afzal Ali
Afzal Ali - avatar
+ 1
:)
14th Jun 2019, 7:39 PM
Afzal Ali
Afzal Ali - avatar