5

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; } 
3
  • 2
    What's wrong with break? Commented Oct 3, 2015 at 7:10
  • 2
    This is C++. Why is swapped an int, when it works as a bool? Commented Oct 3, 2015 at 7:15
  • maybe for ( initial; usual-condition && break-condition; stepper) {...} Commented Oct 3, 2015 at 7:18

3 Answers 3

4

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; 
Sign up to request clarification or add additional context in comments.

1 Comment

Multilevel breaks are, IMO, one of the main use cases for goto in modern C++.
4
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.

6 Comments

initially int swapped = 0;, so it will not enter first for loop.
in second loop, it want pass all the even numbers and for the first odd number doSomethings, then break. but in your code, it doSometings for all even numbers and when find first odd number, it returns from loop.
@alsa I value your comment greatly but I am not sure I understand what you are talking about?
OP's code and the code demonstrated here does not do the same thing, but the idea is applicable.
@GRC , in OP's code second loop is running until first if (arrays[i][j] % 2 != 0) condition occurs then its breaking the loop, but in you code it will skip the second loop if arrays[i][j] % 2 != 0 condition is false. Plz correct that.
|
2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.