Advantage of switch over if-else statement

后端 未结 22 2371
梦谈多话
梦谈多话 2020-11-22 11:08

What\'s the best practice for using a switch statement vs using an if statement for 30 unsigned enumerations where about 10 have an ex

22条回答
  •  醉梦人生
    2020-11-22 12:00

    They work equally well. Performance is about the same given a modern compiler.

    I prefer if statements over case statements because they are more readable, and more flexible -- you can add other conditions not based on numeric equality, like " || max < min ". But for the simple case you posted here, it doesn't really matter, just do what's most readable to you.

提交回复
热议问题