为什么在C ++中使用if-else? [英] Why use if-else if in C++?

查看:220
本文介绍了为什么在C ++中使用if-else?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你可以制作另一个if语句,为什么要使用if-else语句?

Why would you use if-else statements if you can make another if statement?

多个ifs的示例:

input = getInputFromUser()
if input is "Hello"
    greet()

if input is "Bye"
    sayGoodbye()

if input is "Hey"
    sayHi()

使用else-if的示例:

Example with else-if:

input = getInputFromUser()
if input is "Hello"
    greet()

else if input is "Bye"
    sayGoodbye()

else if input is "Hey"
    sayHi()


推荐答案

如果您有非独家条件:

if(a < 100)
{...}
else if (a < 200)
{...}
else if (a < 300)
....

这与没有else的相同代码非常不同...

this is very different from the same code without the "else"s...

这篇关于为什么在C ++中使用if-else?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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