控制#

介绍#

控制功能包括计时、程序流程、条件逻辑和项目终止等功能。这些控制功能允许您暂停执行、创建循环、定义逻辑路径以及结束程序。

函数和关键词#

以下是可用控件列表,包括 API 函数和 C++ 关键字:

  • wait – 将项目暂停一段设定的时间。

  • for – 将一段代码重复执行指定的次数。

  • if – 如果条件为真,则运行一段代码。

  • if/else – 如果条件为真,则运行一个代码块;如果条件为假,则运行另一个代码块。

  • if/else if/else – 按顺序检查多个条件,并运行第一个条件为真的代码块。

  • while – 当条件为真时重复执行一段代码。

  • break – 立即退出当前循环。

  • programStop – 立即结束项目。

wait#

在执行下一行代码之前,暂停项目一段时间。

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

Parameters

范围

类型

描述

time

double

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

units

timeUnits

时间单位: msec (默认)– 毫秒或 seconds

Return Values

此函数不返回值。

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

for#

重复执行一段代码。计数型执行指定次数;范围型执行针对集合中的每个元素执行一次。

Syntax

1 — 计数循环 — 运行固定次数。

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

2 — 基于范围的循环 — 对集合中的每个项目运行一次。

for (type element : collection) {
   // code block
}

Parameters

范围

类型

描述

count

int

代码块重复执行的次数。

type

类型名称

集合中每个项目的数据类型(例如 int double)。

element

variable

用于保存循环当前项值的变量名称。

collection

array

存储待循环遍历元素的数组(或向量)。

Return Values

此控制结构不返回值。

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

// Drive forward different distances
int distances[] = {100, 150, 200};
for (int d : distances) {
  Drivetrain.driveFor(forward, d, mm);
  wait(0.5, seconds);
}

if#

如果条件为 true则运行所附代码块。

Syntax
if (condition) {
  // code block
}

Parameters

范围

类型

描述

condition

bool

语句运行时会计算的表达式或变量。如果其值为 true(假),则执行 if 代码块内的代码;如果其值为 false,则跳过该代码块。

Return Values

此控制结构不返回值。

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#

根据条件是 true 或 false ,确定运行哪个封闭的代码块。

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

Parameters

范围

类型

描述

condition

bool

语句运行时会对其进行求值的表达式或变量。如果其值为 true(假),则执行 if 代码块中的代码;如果其值为 false假),则执行 else 代码块中的代码。

Return Values

此控制结构不返回值。

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#

根据多个条件选择要运行的封闭代码块。

Syntax
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

范围

类型

描述

condition

bool

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

Return Values

此控制结构不返回值。

Notes
  • if 运行其代码块,如果条件为 true。

  • else if 仅当所有先前的条件均为假时才检查附加条件 false。可以使用多个 else if 语句。

  • else 仅当前面的条件都不为真时才运行其代码块 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#

只要条件为 true ,就重复运行代码。

Syntax
while (condition) {
  // code block
}

Parameters

范围

类型

描述

condition

bool

每次迭代前都要进行求值的表达式或变量。如果值为 true假),则循环继续;如果值为 false假),则循环停止。

Return Values

此控制结构不返回值。

Notes
  • 它还可以像“等待直到”一样使用,只需在条件前添加 ! 即可,如下例所示。

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#

立即退出当前循环。如果在嵌套循环中, break 表示退出最内层循环。

Syntax
break;

Parameters

此语句不接受任何参数。

Return Values

此语句不返回值。

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;
  }
}

programStop#

立即结束项目。

Syntax
Brain.programStop();

Parameters

此语句不接受任何参数。

Return Values

此语句不返回值。

Examples
while (true) {

  // Press the screen to end the project
  Brain.Screen.clearScreen(red);
  wait(500, msec);

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

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

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