# \[Quick Coding Tips\] Use Loops in VEXcode VR （Part 1）

**URL:** <https://plc.pd.vex.com/t/quick-coding-tips-use-loops-in-vexcode-vr-part-1/1122>\
**Category:** VEXcode VR\
**Created:** [September 13, 2023, 3:40pm UTC](https://plc.pd.vex.com/t/quick-coding-tips-use-loops-in-vexcode-vr-part-1/1122 "2023-09-13T15:40:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ling\_Zhang](https://plc.pd.vex.com/letter_avatar/ling_zhang/32/6_22734026db12803ebb3e059622ac3534.png) [@Ling\_Zhang](https://plc.pd.vex.com/u/Ling_Zhang)\
**Post date:** [September 13, 2023, 3:40pm UTC](https://plc.pd.vex.com/t/quick-coding-tips-use-loops-in-vexcode-vr-part-1/1122/1 "2023-09-13T15:40:56Z")

</div>

When coding in VEXcode VR, have you ever encountered this situation: the code logic seems perfect, but the VR Robot does not behave as we expected?

Let’s take a look at the following example (Image 1). On the Castle Crasher Playground, we instruct the robot to turn right to check if there is an object in front of it. If an object is detected, the robot will drive forward to it. However, instead of moving toward an object, the robot keeps turning right.

What’s the issue? It arises because the robot only checks for objects once at the very beginning of the project’s execution. By the time the robot aligns with an object, the project has already finished its execution, so the robot will not take any action.

![image](https://plc.pd.vex.com/uploads/default/original/2X/6/6ca9cf6e895096c9065221b463cdde1ddcd513d1.png)

![loop1](https://plc.pd.vex.com/uploads/default/original/2X/8/857423b94313b2cdd1c85a1627db38994e2031ca.gif)

To solve this problem, we can incorporate a forever loop in the project (Image 2), enabling the robot to constantly check for the objects. Once an object is detected, the robot will drive to it.

![image](https://plc.pd.vex.com/uploads/default/original/2X/1/1ff2df43f4ee1b5529ced4a7c657fe639421d10d.png)

![loop2](https://plc.pd.vex.com/uploads/default/original/2X/4/49119a269195e81d048c1f83329cb8e3bec53354.gif)

Loops are an essential component of coding. It allows us to execute a specific block of code over and over again. In other words, **loops offer us a quick and easy way to do something repeatedly.**

**Types of Loops in VEXcode VR**

In VEXcode VR, there are various types of loops we can use when we want to repeat a certain action in code multiple times. The following are the example loop blocks:

![image](https://plc.pd.vex.com/uploads/default/original/2X/8/84348cb2442f9c2425ec6a23cf97c5c16b5ca67c.png)

**Use Different Types of Loops in the VEXcode VR projects**

We have previously discussed the usage of the _forever loop_, the type of loop that continuously runs until we terminate the program, in our first example. Now let’s explore how to use the _repeat loop_, the _while loop_, and the _repeat until loop_ according to the task requirements in our projects.

1. Use the _repeat loop_ when we know how many times the loop will execute.  
A typical example of using the _repeat loop_ is to draw polygons on the Art Canvas+ playground. For instance, if we want to draw a triangle, we just need to repeat the following steps **three times** :  
1). Drive the robot forward for a certain distance;  
2). Turn the robot right 120 degrees.

![image](https://plc.pd.vex.com/uploads/default/original/2X/9/9be7d8b3937121e810224046be8ba39f03624050.png)

![loop1 (1)](https://plc.pd.vex.com/uploads/default/original/2X/9/90d67862c7b1d378ef0f57e226be8a7905c2fa01.gif)

1. Use the _while loop_ or the _repeat until loop_ when we don’t know how many times the loop will execute but want to keep repeating until a specific condition is met. (These two types of loops are also called conditional loops.)  
For example, to solve the Disc Maze (Image 4) in the VEXcode VR, our algorithm can be:  
Repeating the following steps until the DownEye Sensor detects red:  
1). Drive the robot forward;  
2). If the DownEye Sensor detects green, turn right 90 degrees;  
3). If the DownEye Sensor detects blue, turn left 90 degrees.

![image](https://plc.pd.vex.com/uploads/default/original/2X/a/a0db758be71ffbd811ede88b215a6236750bc7c1.png)

We can use either the _while loop_ (Solution 1) or the _repeat until loop_ (Solution 2) to implement this algorithm.

 ![image](https://plc.pd.vex.com/uploads/default/original/2X/0/06c980af3c738f08ed392f32653d3d33cd8b69d0.png)

In addition to these two solutions, we can also use the _forever loop_ and _break_ block to solve this Disc Maze challenge (see Solution 3).

 ![image](https://plc.pd.vex.com/uploads/default/original/2X/b/b687a9ac1ff55402fb08778a50bd9edce32c041b.png)

![conditional](https://plc.pd.vex.com/uploads/default/original/2X/a/ae9ab1c45024a8ccddb3cbbb0a6f9b9d88eefdb7.gif)

**Note:** the _break_ block allows us exit a repeating loop immediately.  
 ![image](https://plc.pd.vex.com/uploads/default/original/2X/3/3b956d43ababd7e145c32df0a43eb4e06f53b115.png)

All these three solutions can successfully navigate the robot from the starting point to the red area. Thus, this is a great example that shows us there is no standard coding solution. Using various loops flexibly according to the task requirement can make our code more efficient and adaptable.

To be continued.

Hope this information helped and please let us know if you have any questions, comments, or feedback. Thank you!
