【问题标题】:How can I have multiple conditions in a while statement? (c++)如何在 while 语句中有多个条件? (c++)
【发布时间】:2015-01-12 04:17:43
【问题描述】:

一个while语句可以同时包含三个条件吗?

例如:

cout << "\n\nHow would you like your parcel shipped?\n (please type standard, express"
    " or same day)" << endl;
cin >> method;
while (method != ("standard" && "express" && "same day"))
{
    cout << "invalid input: please follow the instructions carefully.." << endl;
    cout << "\n\nHow would you like your parcel shipped?\n (please type 'standard',"
        " 'express' or 'same day')" << endl;
    cin >> method;
}

我问的原因是,当我运行这段代码时,它进入了一个无限循环,我无法理解为什么。

【问题讨论】:

  • 这里的&amp;&amp; 运算符有指向字符串的指针作为参数。
  • 当答案出现时,想想你的表达式("standard" &amp;&amp; "express" &amp;&amp; "same day") 以及该表达式产生了什么值。
  • “它进入了一个无限循环,我无法理解为什么。” 好吧,这里的当前答案都没有正确解释这一点。 (而真正尝试这样做的人删除了他们的答案:-()。
  • 什么是methodchar[*]char*std::string?
  • 顺便说一句,别那么endl-happy。在大多数情况下,标准换行符就足够了,更简单,性能更高。

标签: c++ while-loop


【解决方案1】:
while (method != "standard" && method != "express" && method != "same day")

【讨论】:

  • 如果您解释一下他的原始表达式的计算结果,这个答案可能会更好。
【解决方案2】:

直接的问题就是在

method != ("standard" && "express" && "same day")

&amp;&amp; 运算符应用于指向字符的指针,这些指针被转换为 bool,就像它们与空指针进行比较一样(这是任何基本值到 bool 的一般转换v,即v!= 0)。由于它们都是非空的,括号部分的结果是true。通过!=method 进行比较可能无法编译,具体取决于method 的类型。

如果 methodstd::string ...

那么你可以直接将它与字符串文字进行比较,并将条件写成

not (method == "standard" or method == "express" or method == "same day")

但请注意,如果 method 是原始数组,这将不起作用(另请注意:对于 Visual C++ 包含 &lt;iso646.h&gt;,例如通过强制包含,以便使标准 not 编译)。

通常更好的表达条件的方法是使用一组值,并检查该集合中的成员资格。

现在,显示的代码,

cout << "\n\nHow would you like your parcel shipped?\n (please type standard, express or same day)" << endl;
cin >> method;
while (method != ("standard" && "express" && "same day"))
{
    cout << "invalid input: please follow the instructions carefully.." << endl;
    cout << "\n\nHow would you like your parcel shipped?\n (please type 'standard', 'express' or 'same day')" << endl;
    cin >> method;
}

被称为一个半循环,在循环结束,循环之前重复一些代码。

仍然假设methodstd::string,你可以这样重构它:

for( ;; )
{
    cout << "\n\nHow would you like your parcel shipped?\n (please type 'standard', 'express' or 'same day')" << endl;
    cin >> method;
    if( method == "standard" or method == "express" or method == "same day" )
    {
        break;
    }
    cout << "invalid input: please follow the instructions carefully.." << endl;
}

【讨论】:

  • 在我看来,while(true) 对于初学者来说可能比for (;;) 更容易理解
【解决方案3】:
while (method != "standard" && method != "express" && method != "same day")

【讨论】:

    【解决方案4】:
    while (method != ("standard" && "express" && "same day")) { /* ... */ }
    

    当遇到布尔值时,所有这些字符串文字都评估为true,因为在数组衰减后它们不是NULL(这导致指向它们的第一个元素的指针)(NULL 保证与地址不同任何对象)。

    因此,循环相当于:

    while (method != true) { /* ... */ }
    

    我不太明白为什么你的编译器不会大声抱怨这种比较,不管methodchar*char[] 还是std::string
    也许您应该要求遵守标准和警告?
    -Wall -Wextra -pedantic -std=c++14 是一个好的开始。

    关于大肠杆菌:http://coliru.stacked-crooked.com/a/3c2c05950274388e

    如果您想修复您的代码,请为自己进行每次比较(使用strcmp 表示char*char[],简单比较std::string),并将结果与​​布尔运算符结合起来(&amp;&amp;@ 987654337@!)。

    【讨论】:

      【解决方案5】:

      不是你展示的方式,不。每个条件都必须单独测试:

      while ((method != "standard") && (method != "express") && (method != "same day"))
      

      【讨论】:

        【解决方案6】:

        是的,这是可能的;你只需要做对:

        while (method != "standard" && method != "express" && method != "same day")
        {
            // do whatever 
        }
        

        【讨论】:

          【解决方案7】:

          是的:

          cout << "\n\nHow would you like your parcel shipped?\n (please type standard, express or same day)" << endl;
          cin >> method;
          while (method != "standard" && method != "express" && method != "same day")
          {
              cout << "invalid input: please follow the instructions carefully.." << endl;
              cout << "\n\nHow would you like your parcel shipped?\n (please type 'standard', 'express' or 'same day')" << endl;
              cin >> method;
          }
          

          【讨论】:

            【解决方案8】:

            是的,这行得通:

             while (condition && condition && condition) { }
            

            理论上,您可以根据需要/需要链接任意多个条件。

            【讨论】:

              猜你喜欢
              • 2016-02-20
              • 1970-01-01
              • 1970-01-01
              • 2019-01-14
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-03-24
              相关资源
              最近更新 更多