For Each Loop
Place the functions that run as a part of your loop inside {} brackets after defining the alias.
Example
Example

For Loop
Place the functions that run as a part of your loop inside {} brackets after defining the index variable.
Example
Example

While Loop
Place the functions that run as a part of your loop inside {} brackets after defining your conditions.
Example
Example

Break Out Of Loop
Example
Example

Continue to Next Iteration
Example
Example

Iterating Over an Array of Objects
You can access properties of each item in aforeach loop:Nested Loops
You can nest loops for multi-dimensional data:When to Use For vs Foreach
- Use
forwhen you need a fixed number of iterations or an index variable. - Use
foreachto iterate over each item in a list or array.
Notes on Break and Continue
Thebreak and continue statements work in all loop types (for, foreach, and while). Use break to exit the loop early, and continue to skip to the next iteration.