What is better? if..else or multiple simple if

前端 未结 5 1707
旧巷少年郎
旧巷少年郎 2020-12-19 08:44

talking about java performance .. what is better? if..else or multiple simple if

if( condition ) {
  some_code;
  return value;
}
else if( condition ) {
  so         


        
5条回答
  •  一向
    一向 (楼主)
    2020-12-19 09:06

    I'm confident a single if would be better, because whenever a true condition is encountered, it can safely skip the other else alternatives since they won't be executed. If you used multiple ifs, all the subsequent conditions would have to be evaluated anyway (even if, like I think you're supposing, they would be mutually exclusive).

提交回复
热议问题