A while(true) question within an if statement.

Asked 2 years ago, Updated 2 years ago, 83 views

  if (!myDFPlayer.begin(mySoftwareSerial)) {
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while(true);
  }

This is the sample code of the module used in Arduino while(true){...} I saw the format, but I don't remember using only one like that Can you tell me how it's used?

arduino c

2022-09-21 19:16

1 Answers

while (true);

The code above has the same meaning as below.

while (true) {
}

A if statement is also possible in a similar way.

if (true);

Can I ask you one more question? Then is there a reason why you used that when there is nothing in that code?

The code will become an infinite loop, so the code will no longer proceed.

In general, if you fall into an infinite loop, you use the CPU, which affects performance for other programs. However, in situations where only a single program runs, such as embedded, an infinite loop is used to stop the program.

If you have a question, the serial communication fails to start, so it does not work properly even if you move on to the next code, so it seems to be for notifying the user of the problem and stopping the program.


2022-09-21 19:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.