What does "1" in "while()" and "delay()" mean?


asaikk

Recommended Posts

Two completely different things:

while(1): a while(), like if() expects a boolean expression that results in true or false. In DAQFactory, and many other languages, false is represented by 0, and true is represented by any other non-zero number, though 1 is usually used. So, when you do an expression like (8 > 5), the result will be 1. This is handy because you can do regular math with it, for example:

(8 > 5) * 5 + (4 < 3) * 7

would equal 5 because 4<3 evaluates to 0. Replace some of the numbers with variables and you have a useful calculation.

So, when you do while(1), you are basically saying: "while true", which means loop forever, or really until you either stop the sequence or break out of the loop using the break statement. You could easily replace this with 2 or 3 and the loop would work the exact same way. 1, however, is the standard and what programmers expect. while(0) would result in the entire block being skipped and is sometimes used when you want to skip over a block of code and you don't want to bother commenting it out, usually because there are already block level comments in the block.

delay(1): in this case the delay function expects a value corresponding to how long you want to delay, in seconds. So the 1 simply means 1 second, and the sequence will simply pause for 1 second before continuing. You could easily replace this with 2 or 3 or 0.5 and change how long the sequence pauses.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.