Concept Explanation: while (condition) {}

Printer-friendly version


A while loops acts in a very similar manner to an IF statement. Where an IF statement would run the attached code block only once, an IF loop will run the same code multiple times. As long as the conditional statement evaluates to TRUE as the start of the loop, the NXT remains in the loop. If isn't until the conditional statement evaluates to FALSE will it skip the code block and move onto the next part of the program.

The timing of this evaluation is important. A long loop can take a number of seconds to complete and sensor values may fluctuate while the loop block is processed. The conditional statement is only evaluated at the start of the loop. If a sensor value changes after the logical loop, it will only affect the while loop once the entire block of command have been completed.