简体   繁体   中英

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. 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. But in answer to your question this can be achieved by utilizing while loops and logical boolean operators. 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) .

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++;
                    }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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