Switch versus If-Else (a Comparison)
The if-else statement can handle ranges, for instance as follows:
if(marks > 80 && marks <=100)
//statements
else if(marks > 70 && marks <=90)
//statements
But, unfortunately, the switch cannot handle these ranges.
Each switch-case label must be a
single value and that value must be an integer or a character. You cannot even use a floating case
label. As far as the ranges or floating point numbers are concerned, you must use an if-else construct.
On the other hand, if the case labels are integer constants or character constants, then the switch
statement is more efficient, easier to write, and more readily understood.
Do'stlaringiz bilan baham: |