是否可以在 Switch 语句中执行以下操作 - C++? [英] Is It Possible To Do The Following In A Switch Statement - C++?

查看:32
本文介绍了是否可以在 Switch 语句中执行以下操作 - C++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第二个 OOP 课程的编程学生,我有一个简单的问题,我无法在互联网上找到答案,如果有答案,我深表歉意.

I am a programming student in my second OOP class, and I have a simple question that I have not been able to find the answer to on the internet, if it's out there, I apologize.

我的问题是:

是否可以在 switch 语句中使用布尔条件?

Is it possible have Boolean conditions in switch statements?

示例:

switch(userInputtedInt)
{
    case >= someNum && <= someOtherNum
    break;
    // Is this possible?
}

推荐答案

不,这在 C++ 中是不可能的.Switch 语句仅支持整数和字符(它们将被其 ASCII 值替换)进行匹配.如果你需要一个复杂的布尔条件,那么你应该使用 if/else 块

No this is not possible in C++. Switch statements only support integers and characters (they will be replaced by their ASCII values) for matches. If you need a complex boolean condition then you should use an if / else block

这篇关于是否可以在 Switch 语句中执行以下操作 - C++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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