Ελεγχος#

Εισαγωγή#

Ο έλεγχος περιλαμβάνει συναρτήσεις για χρονισμό, ροή προγράμματος, λογική υπό όρους και τερματισμό έργου. Αυτά τα στοιχεία ελέγχου σάς επιτρέπουν να διακόψετε προσωρινά την εκτέλεση, να δημιουργήσετε βρόχους, να ορίσετε λογικές διαδρομές και να τερματίσετε ένα πρόγραμμα.

Συναρτήσεις και λέξεις-κλειδιά#

Παρακάτω είναι μια λίστα με τα διαθέσιμα στοιχεία ελέγχου, συμπεριλαμβανομένων των συναρτήσεων API και των λέξεων-κλειδιών 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#

Διακόπτει προσωρινά το έργο για ένα συγκεκριμένο χρονικό διάστημα πριν από τη μετάβαση στην επόμενη γραμμή κώδικα.

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

Parameters

Παράμετρος

Τύπος

Περιγραφή

time

double

The amount of time to wait, as a positive double.

units

timeUnits

The unit to represent the time: msec (default) – Milliseconds or 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

Όνομα τύπου

The data type of each item in the collection (e.g. 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#

Runs the enclosed block of code if the condition is true.

Syntax
if (condition) {
  // code block
}

Parameters

Παράμετρος

Τύπος

Περιγραφή

condition

bool

An expression or variable that is evaluated when the statement runs. If it is true, the code inside the if block runs; if it is false, the block is skipped.

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#

Determines which enclosed block of code runs based on whether the condition is true or false.

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

Parameters

Παράμετρος

Τύπος

Περιγραφή

condition

bool

An expression or variable that is evaluated when the statement runs. If it is true, the code inside the if block runs; if it is false, the code inside the else block runs instead.

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 runs its block if the condition is true.

  • else if checks additional conditions only if all previous conditions are false. Multiple else if statements can be used.

  • else runs its block only if none of the previous conditions are 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 code as long as the condition is true.

Syntax
while (condition) {
  // code block
}

Parameters

Παράμετρος

Τύπος

Περιγραφή

condition

bool

An expression or variable that is evaluated before each iteration. If it is true, the loop continues; if it is false, the loop stops.

Return Values

Αυτή η δομή ελέγχου δεν επιστρέφει τιμή.

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 the current loop immediately. If in a nested loop, break exits the innermost loop.

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