0

I am using 2 different variables in Arduino Editor, with one incrementing up from 0 to 70, and one decrementing down from 70 to 0. I tried using a for loop for each operator, a nested for loop, and one for loop for both operators. The loop for each operator resulted in each loop finishing before going on to the next. A nested for loop resulted in the loop incrementing by 1, then having the second loop decrement to 0, before the first loop increments by 1 again. The third try will not even compile, and gives me the error code saying that it expects a semicolon before the parenthesis.

for(pos1=70;pos2=0;pos1>=0;pos2<=70;pos1--,pos2++){

I am relatively new to coding but I also looked at forums on how to increment two variables in the same loop which did not seem to help either. Any advice or tips would be appreciated, thank you.

  • 1
    It seems to me that since the other counter is always the value of the first counter subtracted from 70, just get rid of the 2nd counter completely. It serves absolutely no pupose, whatsoever. Instead, just set `int pos1=70-pos2;` (with `pos2` being the only real counter, boringly counting from 0 to 70) in the body of the loop and call it a day. The End. – Sam Varshavchik Jun 07 '22 at 02:37
  • 2
    I am curious -- when you merged your two loops, the first two parts were joined by semicolons (`;`), but the last part was joined by a comma (`,`). Why the difference? – JaMiT Jun 07 '22 at 03:23
  • 1
    Use a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn C++. This is explained in any beginner level C++ book. – Jason Jun 07 '22 at 04:21
  • Thank you @Sam, I tried that and it worked. – Jimmy Johnson Jun 07 '22 at 19:32
  • Hi @JaMiT, when I looked at a [different post](https://stackoverflow.com/questions/1232176/how-do-i-put-two-increment-statements-in-a-c-for-loop) on this site for help, one of the recommendations for putting 2 increments in the same for-loop was to use the comma and to also group the increments in parentheses. However I did not see any success in the parentheses so I took them away, but I did leave the comma. – Jimmy Johnson Jun 07 '22 at 19:35
  • 1
    @JimmyJohnson The part about parentheses in that answer (in fact, everything after the "But is it really a comma operator?" heading) is just an experiment that evolved from comments on the answer; you can ignore that. So you have explained why the comma in the third part. However, why the semicolon for the first two parts? – JaMiT Jun 08 '22 at 04:18

1 Answers1

1

for loop consists of three parts, initialization, evaluation and update. They are separated by semicolon.

In your example you have like 5 parts. It should be

for(pos1=70,pos2=0; (pos1>=0)&&(pos2<=70); pos1--,pos2++){

Look at the semilocons and commas. It complies with:

for (initialization; evaluation; update) {
    // loop code
}
Goswin von Brederlow
  • 11,875
  • 2
  • 24
  • 42
eventHandler
  • 1,088
  • 12
  • 20
  • 1
    Your evaluation part is wrong. Well, it kind of works in this special case, but because of the comma operator, the condition `pos1>=0` is discarded (ignored). It's not a good pattern to apply to other situations. – JaMiT Jun 07 '22 at 03:21