Download Arduino-Based Dataloggers: Hardware and Software
Transcript
while… and do… while loops These conditional execution constructs allow a block of statements to be executed repetitively as long as certain conditions are met, as opposed to for… loops, in which the number of executions is set ahead of time. The statements inside a while… loop may not be executed at all, depending on the initial value of a Boolean expression controlling execution. do… while loops are always executed at least once Sketch 5. Conditional execution loops. because the comparison with the Boolean expression is done at the end of the loop rather than at the beginning. It is possible to write loops that will never terminate, and it is a programmer's responsibility to make sure this doesn't happen. In Sketch 5, the code waits for you to press a key and [Enter] in the serial port monitor. See 2.2.6 Math functions, below, for more information about using the random number generator. The output from this sketch is always the same set of values between 1 and 100. These values are "random" in the sense that they would pass statistical tests for randomness. Note that the loop terminates after seeing a value ≥50 because the test is done at the bottom of the loop. switch construct The switch construct controls execution based on matching a value with a list of integer or character values. (It won't work with real numbers.) The case values in the list don't have to be in any particular order. This construct is often more clear than using a lengthy if… else if… statement. However, unlike if… statements, which execute only the first true branch, each case in the switch construct requires a break; statement to exit when the first match with int value or variable is found; without a break; all the other remaining operations will also be executed. The default keyword provides the opportunity for responding to not finding a match. Often, this response might be to display a message explaining that no match was found. 12