If和Else If之间的区别? [英] Difference Between If and Else If?

查看:310
本文介绍了If和Else If之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么您要使用else if语句,而不要使用多个if语句?例如,这样做有什么区别?

I was wondering why you would use an else if statement, and not multiple if statements? For example, what's the difference between doing this:

if(i == 0) ...
else if(i == 1) ...
else if(i == 2) ...

这:

if(i == 0) ...
if(i == 1) ...
if(i == 2) ...

他们似乎做的完全一样.

They seem to do the exact same thing.

推荐答案

if(i == 0) ... //if i = 0 this will work and skip the following else-if statements
else if(i == 1) ...//if i not equal to 0 and if i = 1 this will work and skip the following else-if statement
else if(i == 2) ...// if i not equal to 0 or 1 and if i = 2 the statement will execute


if(i == 0) ...//if i = 0 this will work and check the following conditions also
if(i == 1) ...//regardless of the i == 0 check, this if condition is checked
if(i == 2) ...//regardless of the i == 0 and i == 1 check, this if condition is checked

这篇关于If和Else If之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆