Skip to main content

For Each Loop

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

For Loop

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

While Loop

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

Break Out Of Loop

Continue to Next Iteration

Iterating Over an Array of Objects

You can access properties of each item in a foreach loop:

Nested Loops

You can nest loops for multi-dimensional data:

When to Use For vs Foreach

  • Use for when you need a fixed number of iterations or an index variable.
  • Use foreach to iterate over each item in a list or array.

Notes on Break and Continue

The break 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.