Loop Block

The Loop block is a container that can hold a sequence of programming blocks. It will make the sequence of blocks inside it repeat. You can choose to repeat the blocks forever, a certain number of times, or until a sensor test or other condition is True.

Only the blocks inside the loop will repeat. After the loop ends, the program will continue with the blocks that are after the loop.
Tips and Tricks
Drag blocks to move them inside a loop. The Loop block will expand to make room for the blocks inside.
Choose the Loop Mode


Mode Selector
Inputs
Loop Index output
Loop Name

Use the Mode Selector to control how the loop will repeat. The different modes specify what condition will make the loop end. For example, you can make the loop repeat a certain number of times, repeat until a sensor data value reaches a certain threshold, or repeat forever. The Inputs available will change depending on the mode. The modes are described below.

You can enter a name for the loop in the Loop Name field on the top of the Loop block. This name is used by the Loop Interrupt block to give you another way to end the loop.

Blocks inside the loop can use the Loop Index output to tell how many times the loop has repeated. See Using the Loop Index Output for more information."
Modes
Unlimited


In the Unlimited mode, the blocks inside the loop are repeated forever. Any blocks placed after the loop will never be reached.
Example 1
This program makes a robot drive in an alternating straight/turn sequence, over and over, until the program is stopped.
Tips and Tricks
Use the Back button on the EV3 to stop a program.
Example 2
This program will display a shut mouth on the EV3 display whenever the Touch Sensor is pressed, and an open mouth otherwise. The loop makes the sensor test in the Switch block repeat continuously.
Count


In the Count mode, the Count input specifies how many times to repeat the blocks inside the loop.
Example
This program makes a robot say “Hello” once, then “Go” six times, then “Bravo” once. The Loop block contains one Sound block and is configured to repeat six times.
Time


In the Time mode, you can specify an amount of time for the loop to repeat in the Seconds input. The time is measured from the beginning of the loop.

The time limit is tested only at the end of the loop sequence. The loop sequence will always execute at least once, and the loop will continue back to the beginning only if the elapsed time at that moment is less than Seconds.
Logic


In the Logic mode, the loop will repeat until the Until True input is True at the end of the loop sequence. The loop sequence will always execute at least once, and the Until True input is tested at the end of each loop iteration.

Use a Data Wire to connect the Until True input to a Logic output of a programming block inside the loop. This output should be True when you want to loop to end."
Example
This program makes a robot drive forward until the Touch Sensor is pressed or the Color Sensor detects black (whichever happens first). The output of a Logic Operations block is used for the Until True input of the loop. When the loop finishes, the robot is stopped.
Tips and Tricks
Many sensor tests involving only one sensor can be tested directly by the Loop block by using the Sensor Modes described below.
Sensor Modes
The Loop block contains several modes that read sensor data and compare it to an input value. There are different modes for each sensor type. Some types of sensor data can be compared to a Threshold Value, and other types can be compared to certain specific values.
Comparing Sensor Data to a Threshold Value
In a mode with a Threshold Value input, the Loop block will repeat the sequence of blocks inside the loop until the threshold comparison is True. For example, you could make a loop repeat until the Color Sensor detects a Reflected Light Intensity value that is less than a threshold value you specify.



Mode Selector
Port Selector
Compare Type input
Threshold Value input

Use the Mode Selector to choose a sensor mode with a Threshold Value input. If the mode uses a port, use the Port Selector to make sure that the port matches the port on the EV3 Brick that the sensor or motor is connected to.

Choose the type of comparison you want to use from the Compare Type list, and enter the Threshold Value.

Each time the sequence of blocks inside the loop finishes, the Loop block will read the sensor data and test the threshold. If the test is False at that moment, the loop will repeat. If the test is True, the loop will end, and the program will continue with the next block after the loop.

For more information on using the different sensor types and what data they provide, see the “Using Sensors” help articles.
Example
This program makes a robot drive forward while repeating three different sounds, until the Color Sensor detects a Reflected Light Intensity of less than 50, then the robot is stopped.
Tips and Tricks
The sensor threshold is only tested at the end of the loop sequence. In the example above, if the Color Sensor briefly passes over a dark color in the middle of the loop, the robot may not see it and not stop.
Comparing Sensor Data to Specific Values
The following sensor modes allow a Loop to compare sensor data to specific input values. The loop will repeat until the sensor data equals a specified input value.
ModeNotes
Brick Buttons - Brick Buttons Loop repeats until one of the selected buttons does the selected action.

See Using the Brick Buttons.
Color Sensor - Color Loop repeats until one of the selected colors is detected.

See Using the Color Sensor.
Infrared Sensor – Remote Loop repeats until one of the selected buttons on the IR Beacon is pressed.

See Using the Infrared Sensor Remote Mode.
Touch Sensor - State Loop repeats until the Touch Sensor is in the selected state.

See Using the Touch Sensor.
Ultrasonic Sensor – Presence/Listen Loop repeats until an Ultrasonic signal is detected.

See Using the Ultrasonic Sensor.
Messaging Loop repeats until the specified message type is received.

See Messaging Block.
Tips and Tricks
The sensor is only tested at the end of the loop sequence. If the sensor data matches the specified input value only briefly while the loop sequence is executing, the loop may not stop.
Using the Loop Index Output
"The Loop Index output at the beginning of a Loop block tells you how many times the loop sequence has executed. You can use the Loop Index inside the loop to make the loop sequence do something different each time the loop repeats.

The Loop Index is 0 during the first time through the loop, and it increases by 1 each time the loop repeats."
Example
This program displays a count of the number of times the touch sensor has been bumped (pressed and then released) on the EV3 Display. A Wait block inside the loop makes the loop wait for each touch sensor bump before repeating, and the Count is used for the Text input of the Display block.
Resizing a Loop
The Loop block will usually expand automatically to make room for new programming blocks that you drag into it. You can also resize a Loop block manually if you need to. To resize a loop, select the Loop block, then drag the resize handles that appear around the outside.
Example
This Loop block is being resized to make room for a second sequence of blocks inside.
Inputs and Output
The inputs available for the Loop block will depend on the mode selected. You can enter the input values directly into the block. Alternatively, the input values can be supplied by Data Wires from the outputs of other Programming Blocks.
InputTypeNotes
Count NumericNumber of times to repeat the loop in Count mode.
Seconds NumericNumber of seconds to repeat the loop in Time mode.
Until LogicIn Logic mode, the loop will end when this input is True.
Compare Type NumericComparison type for a Sensor Mode with a Threshold Value.
0: = (Equal)
1: ≠ (Not Equal)
2: > (Greater Than)
3: ≥ (Greater Than or Equal To)
4: < (Less Than)
5: ≤ (Less Than or Equal To)
Threshold Value NumericValue to compare sensor data to for a Sensor Mode with a Threshold Value.
(Inputs for individual sensor types)See the help on the individual sensor types for more information on the sensor data.
The output of the Loop block is the number of times the loop has repeated. To use the output, use a Data Wire to connect it to a Programming Block inside or after the loop.
OutputTypeNotes
Loop Index Numeric
Loop
Quick links