"<" and ">" working opposite from what I think it should

I was having trouble with a sketch in using the “<” and “>” Boolean operators. They seem to be operating opposite from what I think they should. I have attached a very simple test showing the issue I am having. In this sketch I have defined a numer as “NUM” with a value of one “1” and have incremented it “NUM++” each time the loop runs. I wanted it to stop when NUM is greater than 10. well, it will not run! When I change the “<” to a “>” it runs and stops at 10, just as designed. Why do I have to use a “>” rather than a “<”? It does not seem logical!

//Incremental Number test

void setup(){

Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

Serial.println(“Number Increment Test While NUM<10”);
}

int NUM=0;

void loop()

{

while(NUM<10);
Serial.print(NUM);
Serial.write(’\n’); // Carriage Return
delay(500);
NUM++;
}

What comes after the while statement must be in curly brackets. You have nothing there.

var = 0;
while(var < 200){
  // do something repetitive 200 times
  var++;
}
//Incremental Number test

void setup(){
  
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

Serial.println("Number Increment Test While NUM<10");
}

int NUM=0;

void loop()

{

while(NUM<10){
  Serial.print(NUM);
Serial.write('\n'); //  Carriage Return
delay(500);
NUM++;
}
}

The ; at the end of the while should not be there, you want this:
while(NUM<10)
{
Serial.print(NUM);
Serial.write(’\n’); // Carriage Return
delay(500);
NUM++;
}

Then the stuff in { } will execute until the while condition becomes not true.

Darn fingers are too slow!

Hee, hee!

try this variant

int NUM=0;

void loop()
{
  while(NUM<10)  Serial.println(NUM++);
}

try this variant

Try it, by all means, but then put the conditional expression on a separate line inside curly braces {}, to make it easier to read.