Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use something like a continue statement in nested for loops?

Tags:

I have a class of objects and need to compare one property of each object to the same property of all other objects. If they match, the code needs to do something. This results in two 'for loops' looping through the objects to get that property, and in the second 'for loop', there is a third 'for loop' going through the elements of the property (which is a vector) to compare them. If they match, I need the outer most 'for loop' to abort the current iteration and go on to the next one (I only want the first match with another object to be considered).

I have looked into 'goto' statements and into creating a do{}while() structure, but have not been able to implement them in a way to get the desired result. What I need is something like a 'continue' statement for the outer most loop based on what happens in the conditional statement in the inner most loop.

Which would be a good method to achieve this, and how would it have to be implemented?

Edit: Next to the answer I accepted, I would also recommend Martin Bonner's answer, which also works perfectly fine and doesn't rely on goto.

for (int i = 0; i < max; i++){ Object & object1 = system.getAgent(i); VectorOfStrings object_property1 = object1.getProperty();      for (int j = i + 1; j < max; j++){     Object & object2 = system.getObject(j);     VectorOfStrings object_property2 = object2.getProperty();          for (unsigned int k = 0; k < object_property1.size(); k++){              if (object_property1[k] == object_property2[k]){              //do something              break; //this aborts the inner most loop             //Additionally, I need the outer most loop to move on one iteration             }         }     } } 

So if the conditional statement in the 'k' loop is met, I want the 'i' loop to abort the current iteration and move on to the next one.

Also, since I'm new, the code might be inelegant, but please focus on this specific problem in your answers! Unless a general restructuring of the code might be the solution to the problem of course :)

like image 794
faranzki Avatar asked Dec 16 '16 07:12

faranzki


People also ask

How do you use continue in a nested loop?

The continue statement skips the remainder of the current loop. In the case of nested loops, it skips to the next iteration of the innermost loop. In this case, if you didn't continue, you would execute sum += i+j; in every iteration, where it appears you only want it sometimes.

Can we use continue inside for loop?

The continue keyword can be used in any of the loop control structures. It causes the loop to immediately jump to the next iteration of the loop. In a for loop, the continue keyword causes control to immediately jump to the update statement.

When would you use the continue statement in a for loop?

The continue statement gives you the option to skip over the part of a loop where an external condition is triggered, but to go on to complete the rest of the loop. That is, the current iteration of the loop will be disrupted, but the program will return to the top of the loop.

How do you continue to next iteration in for loop?

You can use the continue statement if you need to skip the current iteration of a for or while loop and move onto the next iteration.


2 Answers

This may be implemented with goto:

for (int i = 0; i < max; i++){     Object & object1 = system.getAgent(i);     VectorOfStrings object_property1 = object1.getProperty();      for (int j = i + 1; j < max; j++){         Object & object2 = system.getObject(j);         VectorOfStrings object_property2 = object2.getProperty();         for (unsigned int k = 0; k < object_property1.size(); k++){             if (object_property1[k] == object_property2[k]){                 //do something                 goto cnt; //this aborts the inner most loop                 //Additionally, I need the outer most loop to move on one iteration             }         }     }     cnt:; } 

This is one of the rare cases when usage of goto really simplifies code.

like image 57
alexeykuzmin0 Avatar answered Nov 28 '22 09:11

alexeykuzmin0


The most readable way to solve this classic problem is almost always to put the nested loops in a function. I don't know what the specific code is suppose to do, but here is some pseudo code you can base a solution on:

bool keep_going = true; for (int i = 0; i < max && keep_going; i++) {   Object& object1 = system.getAgent(i);   keep_going = !compare_objects(object1.getProperty(), i+1, max); } 

where compare_objects is something like this:

inline bool compare_objects (const VectorOfStrings& obj_prop1, size_t begin, size_t end) {   for(size_t i=begin; i<end; i++)   {      Object& obj2 = system.getObject(j);     VectorOfStrings obj_prop2 = obj2.getProperty();      for size_t j = 0; j < obj_prop1.size(); j++)     {       ifobj_prop1[j] == obj_prop2[j])       {         do_something();         return true;       }     }   }    return false; } 
like image 45
Lundin Avatar answered Nov 28 '22 09:11

Lundin