简体   繁体   English

如何在不使用break的情况下退出C ++循环?

[英]How do I exit a loop in C++ without using break?

I'm writing a code to swap integers in an array and I want to know how I can exit a loop without using a break statement and keeping my logic consistent. 我正在编写一个代码来交换数组中的整数,我想知道如何在不使用break语句并保持逻辑一致的情况下退出循环。 Here is my code below: 这是我的代码如下:

int swapped = 0;
if (arrays[0][first] % 2 == 0)
{
     cout << arrays[0][first] << " is odd " << endl;
     for (int i = 1; i < arraycount; ++i)
     {
         for (int j = 1; j < arrays[i][0] + 1; ++j)
         {
             if (arrays[i][j] % 2 != 0)
             {
                  int temp = arrays[i][j];
                  cout << "Array #" << 1 << " value " << arrays[0][first] << " swapped with "
                          << "Array #" << i << " value " << temp;

                  arrays[i][j] = arrays[0][first];
                  arrays[0][first] = temp;
                  swapped = 1;
                  break;
              }
          }
          if (swapped) {
                break;
          }

Use goto [I'll be bashed because of this]. 使用转到[因为这个我会被抨击]。

if (arrays[0][first] % 2 == 0)
{
     cout << arrays[0][first] << " is odd " << endl;
     for (int i = 1; i < arraycount; ++i)
     {
         for (int j = 1; j < arrays[i][0] + 1; ++j)
         {
             if (arrays[i][j] % 2 != 0)
             {
                  int temp = arrays[i][j];
                  cout << "Array #" << 1 << " value " 
                          << arrays[0][first] << " swapped with "
                          << "Array #" << i << " value " << temp;

                  arrays[i][j] = arrays[0][first];
                  arrays[0][first] = temp;
                  goto done;
              }
          }
done:
    something;
for (int i = 1; i < arraycount && !swapped; ++i)
{
     for (int j = 1; j < arrays[i][0] + 1 && !swapped; ++j)
     {
        if(arrays[i][j] % 2 != 0)
          int temp = arrays[i][j];
          cout << "Array #" << 1 << " value " << arrays[0][first] << " swapped with " << "Array #" << i << " value " << temp;
          arrays[i][j] = arrays[0][first];
          arrays[0][first] = temp;
          swapped = 1;
        }
     }
}

this will do the same thing you have in inner loop. 这将在内循环中执行相同的操作。

Using a Break statement does not necessarily make your codes logic inconsistent and breaks are often useful to improve the readability of your code. 使用Break语句不一定会使代码逻辑不一致,并且中断通常对提高代码的可读性很有用。 But in answer to your question this can be achieved by utilizing while loops and logical boolean operators. 但是在回答你的问题时,这可以通过利用while循环和逻辑布尔运算符来实现。 A modified version of your code is below, I have tried to modify it as little as possible so you can still see your code within the example. 下面是您的代码的修改版本,我尝试尽可能少地修改它,以便您仍然可以在示例中看到您的代码。 There are a few logical errors in your code that I have left in the example below that you might want to look into. 我在下面的示例中留下了您可能想要查看的代码中的一些逻辑错误。 In particular the line below will print "is odd" when in fact the number would be even. 特别是下面的行将打印“奇数”,而实际上数字是偶数。 If you where wanting to check if the number arrays[0][first] is odd then the following if statement would be needed if (arrays[0][first] % 2 != 0) instead of if (arrays[0][first] % 2 == 0) . 如果您想要检查数字arrays[0][first]是否为奇数,那么if (arrays[0][first] % 2 != 0)而不是if (arrays[0][first] % 2 == 0)需要以下if语句if (arrays[0][first] % 2 == 0)

Logical Error 逻辑错误

if (arrays[0][first] % 2 == 0)
            {
                    cout << arrays[0][first] << " is odd " << endl;

This is the code without using breaks. 这是不使用中断的代码。

bool swapped = true;
            if (arrays[0][first] % 2 == 0)
            {
                    cout << arrays[0][first] << " is odd " << endl;
                    int i = 1;
                    while ( (i < arraycount) && swapped)
                    {
                            int j = 1;
                            bool if_odd = true;
                            while ((j < arrays[i][0] + 1) && if_odd)
                            {
                                    if (arrays[i][j] % 2 != 0)
                                    {
                                            int temp = arrays[i][j];
                                            cout << "Array #" << 1 << " value " << arrays[0][first] << " swapped with "
                                                    << "Array #" << i << " value " << temp;

                                            arrays[i][j] = arrays[0][first];
                                            arrays[0][first] = temp;
                                            swapped = false;
                                            if_odd = false;
                                    }
                                    j++;
                            }
                            i++;
                    }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM