Repeating actions: Loop blocks in EV3-G Pro Preview

There is a rule in the world of programming: "Duplication should be removed"

There are different tools for removing duplication and one of the most basic ones is the Loop.  Today we would focus on the Loop

To access the full video please subscribe to FLLCasts.com

Subscribe

  • #914
  • 13 Jul 2018

Usually, the robots we make have to perform one and the same movement or a series of movements over and over again. 

For example: we want the robot to move in a square. How can we achieve that? We will put a block for forward movement, then a block for a 90 degree turn and then we will repeat this string three more times. In the end we will have a program with eight blocks. What if we want to make the robot move in a square 100 times, what kind of program will we build – having 800 blocks? Of course not – we will just set the two blocks (the block for forward movement and the turn block) to repeat 400 times.

How can we do that? When we need to repeat actions in programming, we use loops.  

You can take the loop block (cycle) in EV3 from the orange Flow control palette as shown below:

content picture

We insert the blocks we want to repeat in the loop block.

For example, if we want to have an infinite forward-backward movement, the program will have the following structure:

content picture

The loop cycle is by default infinite but in most of our programs, we won’t need an infinite repetition. Let’s take for example the square-movement task – we have to repeat the forward movement block and the turn block four times only. If we want to make a hexagon, we will have to repeat the block six times.

To make the loop repeat a set number of times, click on the infinity symbol (the horizontal 8). Then choose the option Count and write how many times you want it to repeat. Loops have many other conditions for breaking, which we will cover in other lessons.

content picture

Courses and lessons with this Tutorial

This Tutorial is used in the following courses and lessons

Image for Instructors Remote Training
  • 136
  • 280:11
  • 156
Image for How to make the robot move
  • 24
  • 0
  • 11
  • 3d_rotation 0
Image for Robotics with LEGO - Level 1.0 - Adventure in Space
  • 126
  • 123:52
  • 150
Image for Lesson 6 - Exploring the surface of Venus
  • 8
  • 3
  • 6
  • 3d_rotation 1
Image for Level Y. The Younglings (Pre-release)
  • 14
  • 2:49
  • 102
Image for Level A2. "Space exploration". Robotics with LEGO
  • 63
  • 3:02
  • 117
Image for Lesson 1 - Meduim motor and a Loop
  • 9
  • 3
  • 6
  • 3d_rotation 1