Codecademy Logo

Learn to Code with Blockly: Loops

Loop Definition

In programming, a loop is a programming structure that repeats a set of instructions until a specified condition is met. Loops are commonly used in programming because, compared to repeated lines of code, they save time, reduce error, and are easy to read.

A loop to play a sound may look like this:

UNTIL 4 sounds have been played:
    Play a sound

For Loop Definition

In programming, a for loop executes a set of instructions for a specified number of times.

A for loop to add landmarks to a map may look like this:

UNTIL 20 landmarks have been added to the map:
    Add landmark to map

For Each Loop Definition

In programming, a for each loop executes a set of instructions for each item in a given collection.

A for each loop to add a list of destinations to a tourism application may look like this:

FOR EACH destination in the list:
    Add destination to app

While Loop Definition

In programming, a while loop executes a block of code as long as a given condition evaluates to true.

A while loop to make sandwhichs as long as there is bread left may look like this:

WHILE there is bread left:
    Make a sandwich

Learn more on Codecademy