While Loop

A while loop statement in C programming language repeatedly executes a target statement as long as a given condition is true.

Syntax

while (expression) statement

If expression is true, statement will be executed and then expression will be re-evaluated to determine whether or not to execute statement again. It is possible that statement will never execute if expression is false when it is first evaluated

whileloop.png

Example

The expected output for this loop is:

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

Unlike a for loop, the expression must always be there. while loops are used more often than for loops when implementing an infinite loop, though it is only a matter of personal choice.

Infinite Loops
A while loop with expression=1 can execute indefinitely (can leave loop via break statement)

InfiniteWhileLoop.png
© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
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.