控制#

介紹#

控制功能包括計時、程序流程、條件邏輯和專案終止等功能。這些控制功能可讓您暫停執行、建立循環、定義邏輯路徑以及結束程序。

函數和關鍵字#

以下是可用控制項列表,包括 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();
  }
}