Loops

Numeric for loop

The most common and basic type of loop is the numerated for loop. This is the loop that starts at a value, increments by a certain amount (almost always 1), until it hits a certain target, running code that many times. The most common syntax for this is the one found in C/C++/Java/JavaScript:

This will result in the following output:

To go from 1 to 5, set i to 1 initially, and make the end condition i <= 5, rather than i < 5.

This is often used to iterate through an array, like so:

This will output:

While Loops

a while loop keeps running the code body until a certain condition is met.

 

Iterators

Some languages will have specific ways to iterate through collections like arrays. These are normally special functions called iterators.

Since they are language specific, they look different in every language.

A few examples include:

Python:

Rust:

Lua:

Java/C++: