Hi, I've written code to swap integers in multiple arrays and I was wondering is it possible to exit this for loop without the use of break and keeping the logic consistent?
Or you could use sentinel flags as part of your loop conditions, setting their values appropriately within the body of the loop to control whether or not the loop continues.
1 2 3 4 5 6 7 8 9 10
// A loop that only loops 10 times, even though it looks like it will loop 1000 times
bool keep_looping = true;
for (int i = 0; i < 1000 && keep_looping; ++i)
{
std::cout << "I have looped " << i << " times " << std::endl;
if (i > 9)
{
keep_looping = false;
}
}