0

Is switch case same as if statement?

18th Sep 2021, 3:19 AM
Ian Guittap
Ian Guittap - avatar
5 odpowiedzi
+ 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.
18th Sep 2021, 4:12 AM
Ipang
+ 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
18th Sep 2021, 4:39 AM
Patel
+ 1
Thank you
18th Sep 2021, 4:17 AM
Ian Guittap
Ian Guittap - avatar
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.
18th Sep 2021, 3:57 AM
Ipang
0
I mean if statement
18th Sep 2021, 4:02 AM
Ian Guittap
Ian Guittap - avatar