if 和 else if 的区别

查看 55|回复 3
作者:Dongxiaohao   
上班的时候突然想到一个写法
Integer a = 10
===================================================
if (a == 10){
....
}
if (a == 20){
....
}
if (a == 30){
....
}
===================================================
if (a == 10){
....
}else if(a == 20){
....
}else if(a == 30){
....
}
===================================================
上面那个 demo 中,连续的 if 和 else if 有啥区别?个人感觉连续的 if 逻辑更清晰,看起来更工整。else if 有什么必须使用的场景吗?

else, integer, 区别, 写法

isSamle   
一个是走通了不再走了,一个是所有都走一遍吧
Ayanokouji   
超过两个,switch 不是更好吗
sentinelK   
前者即便 a == 10 ,后续的 if 条件也都会执行。
后者只会执行第一条 if 条件。
您需要登录后才可以回帖 登录 | 立即注册

返回顶部