Control#
Introducción#
El control incluye métodos 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.
A continuación se muestra una lista de controles disponibles, incluidos métodos y palabras clave de C++:
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.
esperar#
wait pauses for a specific amount of time before moving to the next method.
Usage:
wait(time, units);
Parámetros |
Descripción |
|---|---|
|
La cantidad de tiempo a esperar, como un entero positivo. |
|
The unit to represent the time:
|
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Stop driving after 1 second
Drivetrain.drive(forward);
wait(1, seconds);
Drivetrain.stop();
}
para#
for iterates over a code block for a set number of times.
Uso:
for (int i = 0; i <= count; i++) {
}
Parámetros |
Descripción |
|---|---|
|
La cantidad de veces que se iterará el bloque de código. |
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);
}
}
si#
if executes the indented block of code if the condition evaluates as true.
Uso:
if (condition) {
}
Parámetros |
Descripción |
|---|---|
|
An expression or variable that is evaluated when the statement runs. If it evaluates as |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
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);
}
}
si/si no#
if and else determine which indented block of code runs based on whether the condition evaluates as true or false.
Uso:
if (condition) {
}
else {
}
Parámetros |
Descripción |
|---|---|
|
An expression or variable that is evaluated when the statement runs. If it evaluates as |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
while (true) {
if (Brain.Screen.pressing()) {
// Turn the screen green when pressed
Brain.Screen.clearScreen(green);
} else {
Brain.Screen.clearScreen();
}
}
}
si/si no si/si no#
The if/else if/else structure selects which indented block of code runs based on conditions:
ifruns 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.
Uso:
if (condition) {
}
else if (condition) {
}
else {
}
Parámetros |
Descripción |
|---|---|
|
An expression or variable that is evaluated when the statement runs. The first condition that evaluates as |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
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);
}
}
mientras#
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.
Uso:
while (condition) {
}
Parámetros |
Descripción |
|---|---|
|
An expression or variable that is evaluated before each iteration. If it evaluates as |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
while (true) {
if (Brain.Screen.pressing()) {
// Turn the screen green whenever it's pressed
Brain.Screen.clearScreen(green);
} else {
Brain.Screen.clearScreen();
}
}
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Wait until the screen is pressed to turn it green
while (!Brain.Screen.pressing()) {
wait(20, msec);
}
Brain.Screen.clearScreen(green);
}
romper#
break exits a loop immediately.
Usage:
break;
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
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;
}
}
}
programaDetener#
programStop ends a running project.
Usage:
Brain.programStop();
Parámetros |
Descripción |
|---|---|
Este método no tiene parámetros. |
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
while (true) {
// Press the screen to stop 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();
}
}
}