How to exit a loop in C ++ without using break?

I am writing code for exchanging integers in an array, and I want to know how I can exit the loop without using the break statement and keeping it logical. 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; } 
+5
source share
3 answers

Using the Break statement does not necessarily make the logic of your codes inconsistent, and breaks are often useful for improving the readability of your code. But in response to your question, this can be achieved through the use of while loops and logical logical operators. A modified version of your code is given below, I tried to change it as little as possible so that you can see the code in this example. There are several logical errors in your code that I left in the example below, which you might want to examine. In particular, the line below will print “odd,” when in fact the number will be even. If you want to check whether the number arrays[0][first] odd, then instead of if (arrays[0][first] % 2 == 0) need the following if if (arrays[0][first] % 2 != 0) .

Logical error

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

This is non-break code.

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

Use goto [because of this I will be broken.]

 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; 
+5
source
 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 as in the inner loop.

+4
source

Source: https://habr.com/ru/post/1232850/


All Articles