Control#
Introducción#
El control incluye funciones para la temporización, el flujo del programa, la lógica condicional y la finalización del proyecto. Estos controles permiten pausar la ejecución, crear bucles, definir rutas lógicas y finalizar un programa.
Funciones y palabras clave#
A continuación se muestra una lista de los controles disponibles, incluidas las funciones de la API y las palabras clave de C++:
wait– Pauses the project for a set amount of time.for– Repeats a block of code a set number of times.if– Runs a block of code if a condition is true.if/else– Runs one block of code if a condition is true, or a different block if it is false.if/else if/else– Checks multiple conditions in order and runs the first block whose condition is true.while– Repeats a block of code while a condition remains true.break– Exits the current loop immediately.programStop– Ends the project immediately.
wait#
Pausa el proyecto durante un tiempo determinado antes de pasar a la siguiente línea de código.
Available Functionsvoid wait(
double time,
timeUnits units );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
The amount of time to wait, as a positive |
|
|
The unit to represent the time: |
Esta función no devuelve ningún valor.
Examples// Stop driving after 1 second
Drivetrain.drive(forward);
wait(1, seconds);
Drivetrain.stop();
for#
Repite un bloque de código. La forma de conteo se ejecuta un número fijo de veces; la forma basada en rangos se ejecuta una vez por cada elemento de una colección.
Syntax1 — Bucle de conteo: se ejecuta un número fijo de veces.
for (int i = 0; i < count; i++) { // code block }
Parameters2 — Bucle basado en rangos: se ejecuta una vez por cada elemento de una colección.
for (type element : collection) { // code block }
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
El número de veces que se repetirá el bloque de código. |
|
Nombre del tipo |
The data type of each item in the collection (e.g. |
|
|
El nombre de la variable que almacenará el valor del elemento actual del bucle. |
|
|
El array (o vector) que contiene los elementos que se recorrerán en el bucle. |
Esta estructura de control no devuelve ningún valor.
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#
Runs the enclosed block of code if the condition is true.
if (condition) {
// code block
}
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
An expression or variable that is evaluated when the statement runs. If it is |
Esta estructura de control no devuelve ningún valor.
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 enclosed block of code runs based on whether the condition is true or false.
if (condition) {
// code block if true
}
else {
// code block if false
}
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
An expression or variable that is evaluated when the statement runs. If it is |
Esta estructura de control no devuelve ningún valor.
Exampleswhile (true) {
if (Brain.Screen.pressing()) {
// Turn the screen green when pressed
Brain.Screen.clearScreen(green);
} else {
Brain.Screen.clearScreen();
}
}
if/else if/else#
Selecciona qué bloque de código incluido se ejecuta en función de varias condiciones.
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
}
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
An expression or variable that is evaluated when the statement runs. The first condition that is |
Esta estructura de control no devuelve ningún valor.
Notesifruns its block if the condition istrue.else ifchecks additional conditions only if all previous conditions arefalse. Multipleelse ifstatements can be used.elseruns its block only if none of the previous conditions aretrue.
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 code as long as the condition is true.
while (condition) {
// code block
}
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
An expression or variable that is evaluated before each iteration. If it is |
Esta estructura de control no devuelve ningún valor.
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#
Exits the current loop immediately. If in a nested loop, break exits the innermost loop.
break;
Esta instrucción no acepta ningún parámetro.
Return ValuesEsta instrucción no devuelve ningún valor.
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;
}
}
programStop#
Finaliza un proyecto inmediatamente.
SyntaxBrain.programStop();
Esta instrucción no acepta ningún parámetro.
Return ValuesEsta instrucción no devuelve ningún valor.
Exampleswhile (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();
}
}