没有条件值的FOR循环“for(int i = 1 ;; i ++)”无法正常工作

时间:2016-01-21 04:09:26

标签: c++ if-statement for-loop

我找到了一个没有CONDITION值的FOR循环的帖子。这是一个循环:

for (INITIALIZATION; CONDITION; AFTERTHOUGHT) 
{
    // Code for the for-loop's body goes here.
}

跳过CONDITION值是不安全的,但是如果使用if/else语句就可以完成。请查看我的for循环:for (int i = 1; ; i++)和内部实现。出于某种原因,我没有使用if/else语句得到正确的逻辑。

#include <iostream>
using namespace std;

int main() {

    int boxes;
    int boxes_for_sale;

    cout << "Enter quantity of the boxes in warehouse: > " << flush;
    cin >> boxes;
    cout << "Enter quantity of the boxes for sale: > " << flush;
    cin >> boxes_for_sale;

    for (int i = 1;; i++) {

        if (boxes < boxes_for_sale) {
            cout << "There are not enough boxes in warehouse!" << endl;
            cout << "Enter quantity of the boxes for sale: > " << flush;
            cin >> boxes_for_sale;
        }
        else
            boxes -= boxes_for_sale;
            cout << "Car N:" << i << " is full\n" << "You have " << boxes << "boxes for sale" << endl;

        if (boxes == 0)
            cout << "Sold out!" << endl;
            break;

    }
    return 0;
}

4 个答案:

答案 0 :(得分:4)

由于您没有指定所需的程序行为,因此我只会说明代码中正在发生的事情。

可能做出意外事情的重要代码片段:

 for (int i = 1;; i++) 

这是完全合法的,但请注意,没有指定终止条件。除非你break出来,否则你将遇到无限循环。

else
    boxes -= boxes_for_sale;
    cout << "Car N:" << i << " is full\n" <<
    "You have " << boxes << "boxes for sale" << endl;

我可以根据缩进来猜测,只有在else之前的if语句为false时才希望两行都执行,但这不是正在发生的事情。

由于您没有使用花括号指定else块,因此只有else之后的第一个语句才会在需要时执行。 cout始终执行。

您遇到同样的问题:

if (boxes == 0)
        cout << "Sold out!" << endl; //will output only if boxes==0
        break; //will break out of loop in any case

答案 1 :(得分:1)

#include <iostream>
using namespace std;

int main() {

    int boxes;
    int boxes_for_sale;

    cout << "Enter quantity of the boxes in warehouse: > " << flush;
    cin >> boxes;
    cout << "Enter quantity of the boxes for sale: > " << flush;
    cin >> boxes_for_sale;

    for (int i = 1;; i++) {

        if (boxes < boxes_for_sale) {
            cout << "There are not enough boxes in warehouse!" << endl;
            cout << "Enter quantity of the boxes for sale: > " << flush;
            cin >> boxes_for_sale;
        }
        else{
            boxes -= boxes_for_sale;
}
            cout << "Car N:" << i << " is full\n" << "You have " << boxes << "boxes for sale" << endl;

        if (boxes == 0)
 {           cout << "Sold out!" << endl;
            break;}

    }
    return 0;
}

答案 2 :(得分:1)

请注意大括号,你需要为每个for循环,if语句和else语句添加大括号。

还要尝试为缩进编程做好准备。

#include <iostream>
using namespace std;

int main() 
{

    int boxes;
    int boxes_for_sale;

    cout << "Enter quantity of the boxes in warehouse: > " << flush;
    cin >> boxes;
    cout << "Enter quantity of the boxes for sale: > " << flush;
    cin >> boxes_for_sale;

    for (int i = 1;; i++) {

        if (boxes < boxes_for_sale) 
        {
            cout << "There are not enough boxes in warehouse!" << endl;
            cout << "Enter quantity of the boxes for sale: > " << flush;
            cin >> boxes_for_sale;
        }
        else
        {
            boxes -= boxes_for_sale;
            cout << "Car N:" << i << " is full\n" << "You have " << boxes <<       boxes for sale" << endl;
        }
        if (boxes == 0)
       {
            cout << "Sold out!" << endl;
            break;
       }

    }
    return 0;
}

答案 3 :(得分:1)

对不起,伙计们缺乏描述。这是我的第一个问题,下次我会更具体。

这是我试图做的。

int main() {

    int boxes_in_warehouse;
    int boxes_for_sale;
    cout << "Enter quantity of the boxes in warehouse: " << flush;
    cin >> boxes_in_warehouse;
    cout << "Enter quantity of the boxes for sale: " << flush;
    cin >> boxes_for_sale;

    if (boxes_in_warehouse < boxes_for_sale) {
        cout << "No, Enter quantity of the boxes for sale: " << flush;
        cin >> boxes_for_sale;
    }

    boxes_in_warehouse -= boxes_for_sale;

    while (boxes_in_warehouse > 0) {
        cout << "You have " << boxes_in_warehouse << " left" << endl;
        cout << "Please enter quantity of the boxes for sale again" << endl;
        cin >> boxes_for_sale;
        boxes_in_warehouse -= boxes_for_sale;
    }

    cout << "we are sold out" << endl;
    return 0;
}

应该运行的代码直到boxes_in_warehouse == 0意味着售罄。

如果您可以优化我的上一个代码,我将很高兴看到您的版本。