В момента ресурсът е наличен само на английски

VEX EDR Intro. Declaring a loop and implementing a square with this loop Pro Preview

The previous tutorial showed you how to copy/paste instructions in RobotC. The current video will introduce you to loops. 

  • #339
  • 13 Apr 2017
  • 4:00


A loop is a repetition of instructions a number of times. Just as we need for the square. In the syntax of the RobotC software there are many ways to define a loop. On of the key words is 'for'

Structure of a loop

The loop is structured as follows

for(i=0; i<4;i++) {

...code to be executed


This structure tells the computer to do the following this:

  1. Check if the value of the variable i is less than 4. 
  2. If it is, then execute the code between the brackets where I've written: "...code to be executed". If it is not, then stop the loop.
  3. Then, increment the value of the variable "i" with 1.
  4. Then start from point 1 again.


Previously we introduced you to implementing a square by repeating the code 4 times and the code for moving and turning. So, if we just copy-paste this code 4 times, the robot will move in a square. But now I would like to introduce you to the concept of loops. And we will use loops so that we can write better easier programs that could be very easy to maintain and that are much more simpler.

This here is the program where we have the square implemented by repeating the code 4 times and now in this language - ROBOTC language we have an abstraction and we have a syntax for declaring a loop. And the syntax is the following. 'for' and here we set i=0 I'll explain this in a moment but for now let's just write it down, i<4; i++ and there is a variable called i. And initially it is with a value 0. Then we just open a bracket, close the bracket. Now, the idea of this code is that for a number of times and this number of times is between i=0 to i < 4 we'll execute the code that is between the brackets. And each time we execute this code i will be incremented by 1. So again, initially i is with a value of 0. Every time we execute the code in the brackets i will be incremented. And this will continue while i is less than 4. Which means that it will be executed 4 times. It will be executed once when i is 0, it will be executed second time when i is 1 then when i is 2 and then when i is 3. So, we will execute this code between the brackets 4 times. And the code that we want to execute and that should be placed between the brackets is actually the code for moving forward and turning right. I'll just cut this code and add it in the loop and the rest of the code I'll just delete. What happens is that the program becomes much more clear. It's easier to maintain. It's easier to find and it's easier to implement. Again, in this loop we can also modify some of the variables. For example, we can say that we want to have this code - move forward and turn executed not 4 but 40 times or 400 times and the robot will execute this code 400 times. For square we just want to execute it 4 times. Let's see how the program works on our robot.

What I hope that you now at least have an idea of are loops and that you can use this tool this abstraction called loops to implement some of your programs. To repeat some of the instructions in your programs a number of times. That's the thing that I hope you know by now.