New Post - Old Question

Consider the demo code below from the UTFT lib. I understand what the purpose of while(1) is, but how does it work? I see that if we press a button on the screen that we print a message, but what happens with the while(1)? Do we exit the while or just go start over again? And if we set the action to call another routine instead of just printing a message, do we return here once that routine is completed? I am having some rather odd behavior in some code built around this demo, and I am wondering if it’s because I don’t understand how this while(1) works? I am used to while (x!=1) { do something), and setting x=0 when a condition is tested and met, to exit the while. What does while(1) really mean? Is while(1) just an infinite loop?

  while(1) 
  {
    if (myTouch.dataAvailable() == true)
    {
      pressed_button = myButtons.checkButtons();


      if (pressed_button==but1) 
        myGLCD.print("Button 1", 110, 220);
        
      if (pressed_button==but2) 
        myGLCD.print("Button 2", 110, 220);
        
      if (pressed_button==but3) 
        myGLCD.print("Button 3", 110, 220);
      
      else if (pressed_button==-1)
        myGLCD.print("None    ", 110, 220);
        
        
        
    } // end if
  } // end while

Yes, while(1) is an infinite loop.

Do we exit the while or just go start over again?

No, you reach the end of the loop, you go to the begin, you test the condition and as the condition is true you repeat again the loop.

Do we exit the while

No. There is nothing that causes that while loop to ever end.

And if we set the action to call another routine instead of just printing a message, do we return here once that routine is completed?

Yes.

I am having some rather odd behavior in some code built around this demo, and I am wondering if it's because I don't understand how this while(1) works?

Only you can answer that.

I am used to while (x!=1) { do something), and setting x=0 when a condition is tested and met, to exit the while.

The break statement makes more sense, to me.

Is while(1) just an infinite loop?

You got it.

The while(1) was put there by someone that doesn't know what loop() does.

http://www.snippets-r-us.com