For Loop

A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

Syntax

for (expression1; expression2; expression3)
statement

expression1 initializes a loop count variable once at start of loop (e.g. i = 0). expression2 is the test condition – the loop will continue while this is true (e.g. i <= 10). expression3 is executed at the end of each iteration – usually to modify the loop count variable (e.g. i++)

forloop.png

Example

The expected output for this for loop is:

Loop iteration 0
Loop iteration 1
Loop iteration 2
Loop iteration 3
Loop iteration 4

It is important to mention that any or all of the three expressions in a for loop may be left blank (semi-colons must remain). If expression1 or expression3 are missing, their actions simply disappear. If expression2 is missing, it is assumed to always be true.

Infinite Loops
A for loop without any expressions will execute indefinitely (can leave loop via break statement)

InfiniteLoops.png
© 2016 Microchip Technology, Inc.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.