Sunday, June 20, 2010

Arduino learning-5

for
The for statement is used to repeat a block of statements enclosed in curly braces a specified number of times. An increment counter is often used to increment and terminate the loop. There are three parts, separated by semicolons (;), to the for loop header:
for (initialization; condition; expression)
{
doSomething;
}
The initialization of a local variable, or increment counter, happens first and only once. Each time through the loop, the following condition is tested. If the condition remains true, the following statements and expression are executed and the condition is tested again. When the condition becomes false, the loop ends.
The following example starts the integer i at 0, tests to see if i is still less than 20 and if true, increments i by 1 and executes the enclosed statements:
for (int i=0; i<20; i++) // declares i, tests if less
{ // than 20, increments i by 1
digitalWrite(13, HIGH); // turns pin 13 on
delay(250); // pauses for 1/4 second
digitalWrite(13, LOW); // turns pin 13 off
delay(250); // pauses for 1/4 second
}
Note: The C for loop is much more flexible than for loops found in some other computer languages, including BASIC. Any or all of the three header elements may be omitted, although the semicolons are required. Also the statements for initialization, condition, and expression can be any valid C statements with unrelated variables. These types of unusual for statements may provide solutions to some rare programming problems.

while
while loops will loop continuously, and infinitely, until the expression inside the parenthesis becomes false. Something must change the tested variable, or the while loop will never exit. This could be in your code, such as an incremented variable, or an external condition, such as testing a sensor.
while (someVariable ?? value)
{
doSomething;
}
The following example tests whether ‘someVariable’ is less than 200 and if true executes the statements inside the brackets and will continue looping until ‘someVariable’ is no longer less than 200.
while (someVariable < 200) // tests if less than 200
{
doSomething; // executes enclosed statements
someVariable++; // increments variable by 1
}
do… while
The do loop is a bottom driven loop that works in the same manner as the while loop, with the exception that the condition is tested at the end of the loop, so the do loop will always run at least once.
do
{
doSomething;
} while (someVariable ?? value);
The following example assigns readSensors() to the variable ‘x’, pauses for 50 milliseconds, then loops indefinitely until ‘x’ is no longer less than 100:
do
{
x = readSensors(); // assigns the value of
// readSensors() to x
delay (50); // pauses 50 milliseconds
} while (x < 100); // loops if x is less than 100
Related Posts Plugin for WordPress, Blogger...
Disclaimer: All the information in this blog is just gathered from different sites in the web and placed here and I am not the owner for these content

Popular Projects

Followers

My Blog List

Give Support

Give Support
Encourage me Through Comments & by Following