+ 1
In what ways will a switch will be worthy as a alternate to nested if ??.
Is switch a real replacement to nested if ?.
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.
+ 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
+ 3
No, it isn't. Although it has a really awkward syntax
+ 2
No, switch can only test if a value equals to something, it can't do comparison.
+ 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");
}
+ 1
So... switch is aint a TOTAL replacement to if else if.
+ 1
:)