控制#
介绍#
控制功能包括计时、程序流程、条件逻辑和项目终止等功能。这些控制功能允许您暂停执行、创建循环、定义逻辑路径以及结束程序。
函数和关键词#
以下是可用控件列表,包括 API 函数和 C++ 关键字:
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#
在执行下一行代码之前,暂停执行一段时间。
Available Functionsvoid wait(
double time,
timeUnits units );
范围 |
类型 |
描述 |
|---|---|---|
|
|
等待时间,以正整数表示。 |
|
|
The unit to represent the time:
|
此函数不返回值。
Examples// Stop driving after 1 second
Drivetrain.drive(forward);
wait(1, seconds);
Drivetrain.stop();
for#
遍历代码块指定次数。
Syntaxfor (int i = 0; i <= count; i++) {
// code block
}
范围 |
类型 |
描述 |
|---|---|---|
|
|
代码块将被执行的次数。 |
此控制结构不返回值。
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.
if (condition) {
// code block
}
范围 |
类型 |
描述 |
|---|---|---|
|
|
An expression or variable that is evaluated when the statement runs. If it evaluates as |
此控制结构不返回值。
Exampleswhile (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.
if (condition) {
// code block if true
}
else {
// code block if false
}
范围 |
类型 |
描述 |
|---|---|---|
|
|
An expression or variable that is evaluated when the statement runs. If it evaluates as |
此控制结构不返回值。
Exampleswhile (true) {
if (Brain.Screen.pressing()) {
// Turn the screen green when pressed
Brain.Screen.clearScreen(green);
} else {
Brain.Screen.clearScreen();
}
}
if/else if/else#
根据多个条件选择要运行的缩进代码块。
Syntaxif (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
}
范围 |
类型 |
描述 |
|---|---|---|
|
|
An expression or variable that is evaluated when the statement runs. The first condition that evaluates as |
此控制结构不返回值。
Notesifruns its block if the condition evaluates astrue.else ifchecks additional conditions only if all previous conditions evaluated asfalse. Multipleelse ifstatements can be used.elseruns its block only if none of the previous conditions evaluated astrue.
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.
while (condition) {
// code block
}
范围 |
类型 |
描述 |
|---|---|---|
|
|
An expression or variable that is evaluated before each iteration. If it evaluates as |
此控制结构不返回值。
NotesIt can also be used like a “Wait until” by adding
!before the condition, as shown in the examples below.
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#
立即退出循环。
Syntaxbreak;
此语句不接受任何参数。
Return Values此语句不返回值。
Exampleswhile (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;
}
}