Skip to main content
Teacher Portal

Programming Loops in VEXcode IQ

Teacher Toolbox icon Teacher Toolbox - The Purpose of this Activity

Programming repetitive behaviors can be accomplished by using [Repeat] or [Forever] loops. In short, [Repeat] blocks allow the programmer to set a specific number of times for the blocks within its loop to repeat. The [Forever] block repeats the blocks within its loop forever, or until the project is stopped. In this activity, students learn about both.

For more information about programming with [Repeat] or [Forever] loops, visit the help information within VEXcode IQ.

The following is an outline of what students will do in this activity:

  • Watch the Using Loops tutorial video.

  • Explore the Repeating Actions example project.

  • Compare and contrast between [Forever] and [Repeat] blocks.

  • Complete the Squared Loops Challenge that asks students to move the Clawbot in a square, and to action the claw and arm before each turn.

For help organizing students for this activity click here (Google / .docx / .pdf).

The Builder in each group should get the hardware required. The Recorder should get the group's engineering notebook. The Programmer should open VEXcode IQ.

Materials Required:
Quantity Materials Needed
1

VEX IQ Super Kit

1

VEXcode IQ

1

Engineering Notebook

1

Using Loops (Tutorial)

1

Repeating Actions example project

This activity will give you the tools to program your robot with repeating behaviors. 

You can use the Help information inside of VEXcode IQ to learn about the blocks. For guidance in using the Help feature, see the Using Help tutorial.VEXcode IQ Toolbar with the Tutorials icon called out in a red box, to the right of the File menu.

Teacher Tips icon Teacher Tips

If this is the students' first time using VEXcode IQ, they can also see the Tutorials in the Toolbar to learn other basic skills. VEXcode IQ Toolbar with the Tutorials icon called out in a red box, to the right of the File menu.

Teacher Tips icon Teacher Tips

Model each of the troubleshooting steps for the students. Remind the students that each group has someone in the role of the builder. That person should be checking these items regularly throughout the activity.

Step 1: Preparing for the activity

Before you begin the activity, do you have each of these items ready? The Builder should check each of the following:

Step 2: Let's start programming with loops.

Start by watching the Using Loops Tutorial video in VEXcode IQ. Tutorial video icon reads Using Loops at the bottom and shows the outline of a loop structure in blocks.

VEXcode IQ contains many different example projects. You’ll use one of them in this exploration. For help and tips on using example projects, check out the Using Examples and Templates tutorial.Tutorial video icon reads Use Example Project and Templates at the bottom and shows a cursor hovering over an icon.

Then, open the Repeating Actions example project. VEXcode IQ Toolbar with the File menu open, and Open Examples selected with a red box. 'Open Examples' is the fourth item in the menu.

The Programmer should complete the following steps: 

  • Open the File menu.
  • Select Open Examples
  • Use the filter bar at the top of the application and select 'Control'. Example project selection window with the Repeating Actions icon highlighted with a red box. The Control filter is selected at the top.
  • Select and open the Repeating Actions example project.Example project icon reads Repeating Actions at the bottom and shows a robot icon with an arrow wrapping around it and reads "times 4" to indicate repetition.
  • Save your project as 'Repeating Actions'. Project name dialog box in the VEXcode IQ Toolbar. Slot 1 is selected and the project name reads Repeating Action.
  • Check to make sure the project name 'Repeating Actions' is now in the window in the center of the Toolbar. 

What is this project actually doing? Example project in the VEXcode IQ Workspace. To the left, the project has a When started block with a Repeat block attached, with the parameter set to 4. Within the loop is a Drive for block set to forward for 300mm and a Turn for block set to right for 90 degrees. To the left a note reads "This program drives a robot in a 300x300mm square by repeating the drive forward and turn commands 4 times."

Do the following in your engineering notebook: 

Predict what the project will have the Clawbot do. Explain each and every action that the robot will complete. 

Teacher Toolbox icon Teacher Toolbox - Answers

Facilitate a classroom discussion to have the students share what they predicted this project will do in their engineering notebook.

Answers will vary, but the students should point out that this project will have the robot drive forward for 300 millimeters and then turn right for 90 degrees, 4 times to complete a square. Instead of using the same 2 blocks 4 times, the repeat block reduces the same actions that 8 blocks would - into 3 blocks. Students' predictions should list all eight behaviors. The repeat block repeats the actions of driving forward and then turning.

Students' engineering notebooks can be maintained and scored individually (Google / .docx / .pdf) or as a team (Google / .docx / .pdf). The previous links provide a different rubric for each approach. Whenever a rubric is included in educational planning, it is good practice to explain the rubric or at least give copies to students before the activity begins.

Teacher Tips icon Teacher Tips

  • Make sure that the students have selected Open Examples from the File menu.

  • Make sure that the students have selected the Repeating Actions Example project. You can point out to students that there are several selections to choose from on the Open Examples page. Remind students that they can refer to the Using Examples and Templates tutorial at any time during the exploration.

  • You can ask students to add their initials or the name of their group to the project name. This will help differentiate the programs if you ask the students to submit them.

  • Since VEXcode IQ has autosave, there is no need to save the project again.

Teacher Tips icon Teacher Tips

  • Make sure that the students have selected Open Examples from the File menu.

  • Make sure that the students have selected the Repeating Actions Example project. You can point out to students that there are several selections to choose from on the Open Examples page. Remind students that they can refer to the Using Examples and Templates tutorial at any time during the exploration.

  • You can ask students to add their initials or the name of their group to the project name. This will help differentiate the programs if you ask the students to submit them.

  • Since VEXcode IQ has autosave, there is no need to save the project again.

Step 3: What is the difference between [Forever] and [Repeat]?

Two code snippets side by side showing the difference in use case between a forever loop (to the left) and a repeat loop (to the right). To the left there is a Comment that reads "use sensor input to determine when to turn" with a forever block with an if then block inside. If the Bumper is pressed the robot will turn right 90 degrees, else it will drive forward. To the right the comment reads "Use a fixed distance to determine when to turn" with a repeat loop set to 4 to drive the robot in a 300mm square.

  • Look over the Repeating Actions example project again. It's also the stack of blocks shown on the right above.
    Notice that the [Repeat] block only has the movements repeating 4 times. Repetitive movements rarely need to repeat forever so a [Repeat] block is used when only a certain number of repetitions is needed.

    If the [Repeat] block was replaced with the [Forever] block, the robot would just repeat in a square forever.
  • A [Forever] block is often used with a conditional statement in order to continuously check to see if the condition is true. In the stack of blocks above attached to the [Forever] block, notice how the robot will drive forward until it meets the condition of bumping into something with the bumper switch. If the bumper switch is pressed, the robot will turn. Otherwise, it will continue driving forward.

    This is a practical use-case of the [Forever] block. Imagine a self-driving sweeper that continues to drive forward until it runs into something, then it turns.

Extend Your Learning icon Extend Your Learning

To further explore how using loops with conditionals, build the Floor Sweeper project.VEXcode IQ project with a When started block and a Forever block attached. Inside the forever block is a Repeat until block with the condition set to Bumper8 pressed, and a Drive forward block inside. Next is a Stop driving block with a turn for block set to right for 90 plus pick random 0 to 90 degrees.

  • Ask students to open the Clawbot (Drivetrain) example project. Example project icon reads Clawbot Drivetrain at the bottom to indicate which template to choose for this project.
  • Ask students to build the project above. 
  • Have the students save the project as 'Floor Sweeper'. Project dialog box in the VEXcode IQ Toolbar. Slot 1 is selected and the project name reads Floor Sweeper. 
  • If students need help saving the project, refer to the Naming and Saving tutorial. VEXcode IQ Toolbar with the Tutorials icon called out in a red box, to the right of the File menu.

If the students need help with any of the blocks, refer them to the Help information or tutorial.

Ask the students to download and run the project to observe how the robot moves. If students need assistance, direct them to the Download and Run a Project tutorial in VEXcode IQ.

Then, begin a class discussion and ask the students to explain why the [Forever] block was used instead of a [Repeat] block.

The students should note that a [Forever] block is used because this project continuously checks to see if the bumper switch is being pressed. 

Teacher Toolbox icon Teacher Toolbox - Setting Up the Challenge

Have students program the robot to move in a square, have the robot display colors, make sounds, and move the Claw and Arm! The students will take the Repeating Actions example project one step further by incorporating the Arm and Claw as well as sounds and colors.

  • For information on how to organize a classroom for an activity, click here (Google / .docx / .pdf).

Step 4: The Stop and Go Challenge!

Sketched path of the movement of the robot showing a green square with arrows indicating moving counterclockwise in a square. At each corner there is a doublesided orange arrow indicating a motion at this point in the path.

  • Have your Clawbot drive in a square. 
  • Before each turn: 
    • The claw must be opened and closed.
    • The arm must be raised and lowered.
    • The Touch LED must show at least one color. 
    • At least one sound must play. 
  • The Clawbot cannot drive along a side of the square more than once. 
  • You can use the Repeating Actions example project as a starting point but save it as StopAndGo before making any changes.Project name dialog box in the VEXcode IQ Toolbar. Slot 1 is selected and the project name reads Stop and Go.

In your engineering notebook, plan the following:

  • Have the Driver and Recorder plan out your solution and predict what each block in your project will have the Clawbot do.
  • Have the Programmer Download and run your project to test it before submitting it.
  • Have the Builder make changes to the project as needed and communicate with the Recorder to take notes about what was changed during testing.

Teacher Toolbox icon Teacher Toolbox - Solution

The following is a potential solution to the Stop and Go Challenge:VEXcode IQ sample solution with a When started block and a repeat block set to 4 attached. There are 9 blocks within the repeat block that read, in order, Set TouchLED to red, Drive forward for 300 mm, Spin claw motor open for 70 degrees, spin arm motor up for 360 degrees, spin claw motor close for 70 degrees, spin arm motor down for 360 degrees, set touch led color to green, play sound siren, turn right for 90 degrees.

You can provide students with a programming rubric for scoring their projects (Google / .docx / .pdf).

Students' engineering notebooks can be maintained and scored individually (Google / .docx / .pdf) or as a team (Google / .docx / .pdf).

Motivate Discussion icon Motivate Discussion - Reflecting on the Activity

Q: Did the Clawbot move as you expected it to?
A: Answers will vary; however, the goal of this question is to promote cognitive thinking. Students began by predicting behaviors before testing them, documenting their results, and reflecting.

Q: What is the biggest challenge in moving the Arm and Claw in addition to the Repeating Actions example project?
A: While answers may vary, a common response should be that the students might have had to test how many degrees the Claw should open and close and how many degrees the Arm should move up and down.

Q: How does using the repeat increase the efficiency of the project?
A: Without the [Repeat] loop, the Clawbot would only perform the behaviors as determined by the blocks in the project once. The [Repeat] loop tells the Clawbot to perform all of the blocks 4 times to form a square.