How to use greater than or equal in a switch statement

后端 未结 8 2499
忘掉有多难
忘掉有多难 2021-02-19 04:10

What is the best way to check if variable is bigger than some number using switch statement? Or you reccomend to use if-else? I found such an example:

int i;

i         


        
8条回答
  •  无人共我
    2021-02-19 04:58

    I would strongly recommend a if(var1>var2){}else if (var1==var2) {} else {} construct. Using a switch here will hide the intent. And what if a break is removed by error?

    Switch is useful and clear for enumerated values, not for comparisons.

提交回复
热议问题