Type Here to Get Search Results !

How To Use Control Statement For Loop Function In Arduino Programming

For loop, is an iterative statement that allows to repeatedly execute lines of code in a loop a specified number of times. for loop is based on a counter, or a specific variable that is incremented each time the loop is repeated. the basic syntax for a for loop will be as follow, the for loop begins with three statements that include: variable declaration, conditional statement, and incremental statement. The first is the counter variable declaration and it is run only once the first time through the loop. The second statement is the conditional statement using comparative operators just like we will use in the if statement and is tested each time through the loop.



If the condition remains true, the code closed by the curly braces will be executed. if however, the condition returns false, the for loop will end the third one is being used increments the counter variable each time the enclosed block of code is executed.

Let’s say we wanted to blink a LED three times quickly, so we could use a for loop similar to the following code. Now, in this sample code, the first time through the for loop, we declare an integer type variable. That will be used as index or counter and assign the value 0 each time through the loop, the conditional statement will be checked to see if the condition remains true so in our example, it will check if "i" is less or equal to the value 3.

Meaning turning ON pin 13, waiting for 300 milliseconds, turning pin 13 OFF, then waiting another 300 milliseconds. now each time through the loop, after all of the statements within the curly braces have been executed, the variable "i" is incremented using the increment statement: i++ ok, it is also can be decremented meaning i--

You have several combinations to use this index or counter. Please watch this video tutorial to understand it better.



Post a Comment

0 Comments