Control#

Introduction#

Control includes methods for timing, program flow, conditional logic, and project termination. These controls let you pause execution, create loops, define logic paths, and end a program. Below is a list of available controls, including methods and C++ keywords:

  • wait – Pauses execution for a given number of milliseconds or seconds.

  • for – Repeats code for each item in a sequence.

  • if – Executes code if a condition is true.

  • if/else – Runs different code depending on a condition.

  • if/else if/else – Checks multiple conditions in order.

  • while – Repeats code while a condition is true.

  • break – Exits a loop immediately.

  • programStop – Ends the running program.

wait#

wait pauses for a specific amount of time before moving to the next method.

Usage:
wait(time, units);

Parameters

Description

time

The amount of time to wait, as a positive integer.

units

The unit to represent the time:

  • msec (default) – Milliseconds
  • seconds.
int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Stop driving after 1 second
  Drivetrain.drive(forward)
  wait(1, seconds)
  Drivetrain.stop()
}

for#

for iterates over a code block for a set number of times.

Usage:

for (int i = 0; i <= count; i++) {

}

Parameters

Description

count

The amount of times that the code block will be iterated through.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Drive in a square
  for (int i = 0; i <= 4; i++) {
    Drivetrain.driveFor(forward, 100, mm);
    Drivetrain.turnFor(right, 90, degrees);
  }
}

if#

if executes the indented block of code if the condition evaluates as true.

Usage:

if (condition) {
  
}

Parameters

Description

condition

An expression or variable that is evaluated when the statement runs. If it evaluates as true, the code inside the if block executes; if it evaluates as False, the block is skipped.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Turn right when Check button pressed
  while (true) {
     if (Brain.buttonCheck.pressing()) {
      Drivetrain.turnFor(right, 90, degrees);
    } 
  }
}

if/else#

if and else determine which indented block of code runs based on whether the condition evaluates as true or false.

Usage:

if (condition) {
  
}
else {
  
}

Parameters

Description

condition

An expression or variable that is evaluated when the statement runs. If it evaluates as true, the code inside the if block executes; if it evaluates as false, the code inside the else block executes instead.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Only turn right when the Check button is pressed
  while (true) {
    if (Brain.buttonCheck.pressing()) {
      Drivetrain.turn(right);
    }
    else {
      Drivetrain.stop();
    } 
  }
}

if/else if/else#

The if/else if/else structure selects which indented block of code runs based on conditions:

  • if runs its block if the condition evaluates as true.

  • else if checks additional conditions only if all previous conditions evaluated as false. Multiple else if statements can be used.

  • else runs its block only if none of the previous conditions evaluated as true.

Usage:

if (condition) {
  
}
else if (condition) {
  
}
else {
  
}

Parameters

Description

condition

An expression or variable that is evaluated when the statement runs. The first condition that evaluates as true determines which block executes; if none are true, the else block runs.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Turn right or left depending on the button pressed
    while (true) {
    if (Brain.buttonRight.pressing()) {
      Drivetrain.turn(right);
    }
    else if (Brain.buttonLeft.pressing()) {
      Drivetrain.turn(left);
    }
    else {
      Drivetrain.stop();
    }    
  }
}

while#

while repeatedly runs methods as long as the condition is true. It can also be used like a “Wait until” by adding ! before the condition, as shown in the example below.

Usage:

while (condition) {
    
}

Parameters

Description

condition

An expression or variable that is evaluated before each iteration. If it evaluates as true, the loop continues; if it evaluates as false, the loop stops.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Keep the screen green while the robot is moving
  Drivetrain.driveFor(forward, 200, mm, false);
  wait(0.2, seconds);
  while (Drivetrain.isMoving()) {
    Brain.Screen.clearScreen(green);
    wait(50, msec);
  }
  Brain.Screen.clearScreen(black);
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Move in a square pattern forever
  while (true) {
    Drivetrain.driveFor(forward, 150, mm);
    Drivetrain.turnFor(right, 90, degrees);
    wait(0.2, seconds);
  }
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Wait until a button is pressed to stop driving
  Drivetrain.drive(forward);
  while (!Brain.buttonCheck.pressing()) {
    wait(0.2, seconds);
  }
  Drivetrain.stop();
}

break#

break exits a loop immediately.

Usage:
break;

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Turn right until the check button is pressed
  while (true) {
    Drivetrain.turn(right);
    if (Brain.buttonCheck.pressing()) {
      break;
    }
  }
  Drivetrain.stop();
  Brain.Screen.print("Loop exited.");
}

programStop#

programStop ends a running project.

Usage:
Brain.programStop();

Parameters

Description

This method has no parameters.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Stop the project when the check button is pressed
  while (true) {
    Drivetrain.turn(right);
    if (Brain.buttonCheck.pressing()) {
      brain.programStop();
    }
  }
  Drivetrain.stop();
  Brain.Screen.print("Loop exited.");
}