0
Is switch case same as if statement?
5 Antworten
+ 3
Both are for evaluation
But each has their plus/minus
`if` and its `else if` companion allows flexibility. Because we can evaluate multiple conditions chained by logical operators, and we can also use ranges for matching values.
Note that `else` does NOT accept a condition to be evaluated.
`switch` allows simplicity, as it is commonly used to evaluate fixed value, rather than an evaluable expression.
Some languages allows use of ranges in `switch`, but not a majority.
+ 2
Following are the difference between if and switch statement:
switch can only test for equality whereas if can test for any Boolean expression.
switch tests the same expression against constant values while if-else-if ladder can use different expression involving unrelated variables.
switch expression must only evaluate to byte, short, int, char, String or an enum. if doesn’t have such limitations.
A switch statement will run much faster than the equivalent program written using the if-else-if ladder
+ 1
Thank you
0
No ...
switch is for evaluation purposes.
for...loop is for repetition purposes.
I think you need to review the chapters around those subjects ...
* Thread title had been edited.
0
I mean if statement