Understanding expected ')' before ';' token

For the most part i have been able to fumble my way around code and get it to work but I am stumped on this one. I am trying to use an rotary incremental encoder to measure degrees. But there have been a few times i have found this error (expected ‘)’ before ‘;’ token) while trying stuff and i cannot figure it out.

Arduino: 1.8.9 (Linux), Board: “Arduino/Genuino Uno”

/tmp/arduino_modified_sketch_680805/Basic.pde: In function ‘void loop()’:
Basic:33:15: error: expected ‘)’ before ‘;’ token
if (test = 0; test <= 21; test++) { //test = 0; test <= 21; test++
^
Basic:33:35: error: expected ‘;’ before ‘)’ token
if (test = 0; test <= 21; test++) { //test = 0; test <= 21; test++
^
exit status 1
expected ‘)’ before ‘;’ token

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

/* Encoder Library - Basic Example
   http://www.pjrc.com/teensy/td_libs_Encoder.html

   This example code is in the public domain.
*/

#include <Encoder.h>

// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEnc(2, 3);
//   avoid using pins with LEDs attached

void setup() {
  Serial.begin(57600);
  Serial.println("Basic Encoder Test:");
}

long oldPosition  = -999;
int test = 0;


void loop() {
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
    newPosition = test;
  }

  if (test = 0; test <= 21; test++) {     //test = 0; test <= 21; test++
    
    Serial.println(test);
  }



  //    myEnc.write()

  //int fancyMath(){
  //
  //  int answer;
  //
  //  answer = test / 360;
  //  return answer;
  //  }


  //  if(test =

  //if (test=0; test <= 21; test++){
  //     Serial.println(test);
  //  }

  // if (oldPosition > 4095) {
  //     oldPosition++;
  //    Serial.println(newPosition);
  //}
}

if (test = 0; test <= 21; test++) {  Did you mean “for”, instead of “if”?

The problem is a simple syntax error...forrest-for-the-trees type. Also, when you have long blocks of code that you want commented out, why not use the block comment:

/*

int i;

*/

It's easier to use.

Another common shortcut is:

#define DEBUG             // Comment this line out when done debugging
// a bunch of code...

#ifdef DEBUG
Serial.print("x = ");
Serial.println(x);
#endif

When the #define is active, you see the debug print statements. If you comment out the define for DEBUG, the debug statements are not compiled into the code. This makes it easy to have source file with debugging info in it, but remove the info when distributing.

Thank you!

I'll have to play with that debugging code. I use the // alot when im in a hurry and trying different things. I always go back and make sure my commenting is corrected to code blocks when it is done.

And yes i believe it was supposed to be a for loop and i did not get the expected output.

freePerro:
And yes i believe it was supposed to be a for loop and i did not get the expected output.

What was the expected output?
When you say "if" the compiler expects "if ( expression )" and you gave it "if ( expression ;". The compiler says "expected ')' before ';' token". Seems reasonable to me.
On the other hand I stared at that code and didn't see anything wrong. :frowning:

freePerro:
I always go back and make sure my commenting is corrected to code blocks when it is done.

Famous last words. IMO, it is much better to comment as you code. Think of it as part of the code (the part for humans to read who are the ones who will have to maintain/change the code). It is far to easy to skip this and then never quite get back to "cleaning up the comments"

YMMV.