I got this from this tutorial (ESP32 with HC-SR04 Ultrasonic Sensor with Arduino IDE | Random Nerd Tutorials) and I don't understand what it means. Any ideas?
for (;;)
is apparently the same as while (1)
Yes, it is an endless loop.
it's an infinite for loop
try this:
void setup() {
Serial.begin(115200);
Serial.println("Starting");
for (;;);
Serial.println("You will never see this");
}
void loop() {}
see for loop - cppreference.com and the point where they mention
3) Empty condition is equivalent to while(true)
From: Arduino For Loop - How you can use it the Right Way.
Infinite For loop
The format of the infinite for loop is slightly strange:
for(;;) { // Do something forever }
The code operates exactly the same as the while loop. The two semicolons just mean that the initialiser, the conditional test and the iterator are void i.e. they do not exist. The compiler just leaves these parts out, and all that is left is the assembler jump back instruction. So the loop continues forever.
Thanks, and is there a way to exit the loop.
nope reset it (also it will freeze the arduino because arduinos cant multitask)
34 posts were merged into an existing topic: freeRTOS task without a loop?
break;
It's the
for (; ; );
thread.
a7
@foltynfan,
It has been suggested that the discussion about free RTOS be moved to a separate topic as it is not directly related to your original question. As this is your topic please tell me if you would like that discussion split from your topic of if you are happy for it to remain here. I will do as you wish.
Thanks,
I would like to split the discussion.
Moved to freeRTOS task without a loop?
Forcing a "break;
" with a button:
void setup()
{
Serial.begin(115200);
Serial.println("Starting");
pinMode(2,INPUT_PULLUP);
for(;;) if(digitalRead(2) == LOW) break;
Serial.println("You will never see this");
}
void loop() {}
Try the sketch in Wokwi:
most people would do
void setup() {
Serial.begin(115200);
Serial.println("Starting");
pinMode(2,INPUT_PULLUP);
while(digitalRead(2) != LOW) ; // active wait until button is pressed
Serial.println("You will see this if you press the button");
}
void loop() {}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.