Odd While Loop Errors

I am building a walking robot for one of my classes and using a photo interrupter sensor (GP1A57HRJ00F) to count motor cycles.

I have attached an interrupt to increment a counter every time an object passes through the sensor (ie wire attached to motor shaft).

attachInterrupt(0, Check, RISING);

 void Check()
{
    counter1++;
}

Anyways, I need the robot to walk a certain number of steps, which corresponds to a certain number of motor cycles. To do this I have a while loop (as seen below) which simply delays, until the counter reaches a certain value.

while(counter1 <=9){ //delay until desired number of revolutions
 }

The problem is even when the counter reaches that certain value, it will not exit out of the while loop. However, when I put in a print statement for the counter inside the loop, suddenly everything works as it should.

I have tested the photo interrupter thoroughly to make sure it is working. I have put delays before the while loop to ensure the counter is 10+ when it hits the while loop and verified it bypasses it properly. Therefore, I think this is an oddity in the structure of the while loop

I'm really new to microcontrollers and Arduino, so any suggestions/comments would be really appreciated.

Thanks,
Mitch

Also I can post the remaining code I have if need be.

How is counter1 declared?
It should be 'volatile'

I declared counter 1 as below

//COUNTER
int counter1;

If you don't mind, could you explain what declaring it as volatile does and why my code would work before when I had the print statement in the loop.

Thanks.

It makes sure accesses to it don't get optimised-out.
Your while loop has no reason to suspect that the value of counter1 can ever change, so it has no reason to reload the contents.
Volatile tells the compiler that the value can change because of some external influence, like an interrupt

I made the change and it worked.

Thanks for the help. I appreciate it.