控制#

介绍#

Control includes functions for timing, program flow, conditional logic, and project termination. These controls let you pause execution, create loops, define logic paths, and end a program.

Functions and Keywords#

以下是可用控件列表,包括方法和 C++ 关键字:

Below is a list of available controls, including API functions and C++ keywords:

  • wait – Pauses execution for a specified amount of time.

  • for – Repeats code a specific number of times.

  • if – Executes code when a condition evaluates to true.

  • if/else – Executes one block of code if a condition is true, otherwise runs an alternate block.

  • if/else if/else – Evaluates multiple conditions in sequence.

  • while – Repeats code while a condition remains true.

  • break – Immediately exits the nearest enclosing loop.

wait#

Pauses execution for a specific amount of time before moving to the next method.

Available Functions
void wait(
  double time,
  timeUnits units );

Parameters

Parameter

Type

描述

time

double

等待时间,以正整数表示。

units

timeUnits

The unit to represent the time:

  • msec (default) – Milliseconds
  • seconds
Return Values

This function does not return a value.

Examples
// Stop driving after 1 second
Drivetrain.drive(forward);
wait(1, seconds);
Drivetrain.stop();

for#

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

Available Functions
for (int i = 0; i <= count; i++) {
   // code block
}

Parameters

Parameter

Type

描述

count

int

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

Return Values

This control structure does not return a value.

Examples
// Drive in a square
for (int i = 0; i < 4; i++) {
  Drivetrain.driveFor(forward, 100, mm);
  Drivetrain.turnFor(right, 90, degrees);
}

if#

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

Available Functions
if (condition) {
  // code block
}

Parameters

Parameter

Type

描述

condition

bool

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.

Return Values

This control structure does not return a value.

Examples
while (true) {
  if (Brain.Screen.pressing()) {
    // Display a message once the screen is pressed
    Brain.Screen.clearScreen();
    Brain.Screen.setCursor(1, 1);
    Brain.Screen.print("Screen pressed!");
    break;
  }
  wait(5, msec);
}

if/else#

Determines which indented block of code runs based on whether the condition evaluates as true or false.

Available Functions
if (condition) {
  // code block if true
}
else {
  // code block if false
}

Parameters

Parameter

Type

描述

condition

bool

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.

Return Values

This control structure does not return a value.

Examples
while (true) {
  if (Brain.Screen.pressing()) {
    // Turn the screen green when pressed
    Brain.Screen.clearScreen(green);
  } else {
    Brain.Screen.clearScreen();
  }
}

if/else if/else#

Selects which indented block of code runs based on multiple conditions.

Available Functions
if (condition) {
  // code block if first condition is true
}
else if (condition) {
  // code block if second condition is true
}
else {
  // code block if no conditions are true
}

Parameters

Parameter

Type

描述

condition

bool

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.

Return Values

This control structure does not return a value.

Notes
  • 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.

Examples
while (true) {
  // Turn screen blue if pressed on the left half
  if (Brain.Screen.pressing() && Brain.Screen.xPosition() < 240) {
    Brain.Screen.clearScreen(blue);

  // Turn screen green if pressed on the right half
  } else if (Brain.Screen.pressing() && Brain.Screen.xPosition() > 240) {
    Brain.Screen.clearScreen(green);

  } else {
    Brain.Screen.clearScreen();
  }

  wait(20, msec);
}

while#

Repeatedly runs methods as long as the condition is true.

Available Functions
while (condition) {
  // code block
}

Parameters

Parameter

Type

描述

condition

bool

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.

Return Values

This control structure does not return a value.

Notes
  • It can also be used like a “Wait until” by adding ! before the condition, as shown in the examples below.

Examples
while (true) {
  if (Brain.Screen.pressing()) {
    // Turn the screen green whenever it's pressed
    Brain.Screen.clearScreen(green);
  } else {
    Brain.Screen.clearScreen();
  }
}

// Wait until the screen is pressed to turn it green
while (!Brain.Screen.pressing()) {
  wait(20, msec);
}

Brain.Screen.clearScreen(green);

break#

Exits a loop immediately.

Available Functions
break;

Parameters

This statement does not accept any parameters.

Return Values

This statement does not return a value.

Examples
while (true) {

  // Press the screen to pause on a color
  Brain.Screen.clearScreen(red);
  wait(500, msec);

  if (Brain.Screen.pressing()) {
    break;
  }

  Brain.Screen.clearScreen(green);
  wait(500, msec);

  if (Brain.Screen.pressing()) {
    break;
  }
}