1

Is it possible for a for loop with 2 variables to stop incrementing only one of the variable when a condition is met ? For example

for(int i = 0, j = 0; i < 5 && j < 10; i++, j++)
{
    cout << i << " " << j << endl;
}

and the output would look something like

0 0
1 1
2 2
3 3
4 4
4 5
4 6
4 7
4 8
4 9

This is my actual code. I wanted the condition for both variables

cout << sp.dets.size() << " " << gt.groundtruth.size() << endl;
for (int i = 0, j = 0; i < sp.dets.size() && j < gt.groundtruth.size(); j < gt.groundtruth.size() ? j++ : j, i < sp.dets.size() ? i++ : i)
{
    cout << i << " " << j << endl;
}

sp.dets.size = 0

gt.groundtruth.size() = 8

It would be nice if the solution works for any number i.e. i > j or i < j or i = 0 or j = 0

5
  • Saying "i < 5 && j < 10; i++, j++" is counterintuitive by logic nature of it. Commented Nov 21, 2016 at 8:07
  • @abdul, you can define variable inside loop conditions in c99 as well. Commented Nov 21, 2016 at 8:12
  • kong, are you programming in C or C++? You should re-tag your question appropriately. Commented Nov 21, 2016 at 8:13
  • Hi I am programming in C++. the tag is correct Commented Nov 21, 2016 at 8:14
  • @StoryTeller I replied to a comment, but it's deleted Commented Nov 21, 2016 at 8:14

4 Answers 4

3

You can use ternary statement to increment variable i value. Like below:

for(int i = 0, j = 0; i < 5 && j < 10; j++, i<4? i++: i)
{
    cout << i << " " << j << endl;
}

This will output the expected result.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks ! What if I wanted the condition for both variables ? I edited my code above to show another example. Seems not to work
the i<5 part of the for condition can be removed because i can never get larger than 4
@ karsten: yes right, there is no need for that. @kong: i think you should compare it to decremented value j < gt.groundtruth.size() -1 instead of j < gt.groundtruth.size() for both i and j .That seems to be the reason second example didnt worked for you.
0

Something like this may work,

int k = 0;
int val = 4;
for(int i = 0, j = 0; i < 10 && j < 10; i++, j++){   
if (i>=val){
    k=val;
    cout << k << " "<< j << endl;
}
else 
cout << i << " " << j << endl;
}

Prints this,

0 0
1 1
2 2
3 3
4 4
4 5
4 6
4 7
4 8
4 9

Comments

0

Don't try to be overly clever by trying to fit all the logic in one line. It't more important that your code can be read by others (and yourself in two weeks' time) than saving a single line. If the incrementing logic of the two variables is more complex than just increasing by one for each loop, put them in seperate lines, like this:

while (i<5 && j<10) {
  ...
  // complicated expression calculating new i
  // complicated expression calculating new j
}

In your case, you might also flip the logic around:

for (int i=0; i<max(sp.dets.size(), gt.groundtruth.size(); i++) {
   cout << min(i, sp.dets.size()-1)
        << ","
        << min(i, gt.groundtruth.size()-1)
        << endl;
}

Comments

0

You can use ternary operator on both variables. Just be sure to change your condition to break only when both reach their destined value

for (int i = 0, j = 0;
     i != 5 || j != 10;)
{
   i += i < 5  ? 1 : 0;
   j += j < 10 ? 1 : 0;
}

You'll note I moved the increment into the loop body, this improves readability, in my opinion.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.