控制#
介紹#
控制功能包括計時、程序流程、條件邏輯和專案終止等功能。這些控制功能可讓您暫停執行、建立循環、定義邏輯路徑以及結束程序。
函數和關鍵字#
以下是可用控制項列表,包括 API 函數和 C++ 關鍵字:
wait– 將項目暫停一段設定的時間。for– 將一段程式碼重複執行指定的次數。if– 若條件為真,則執行一段程式碼。if/else– 如果條件為真,則執行一個程式碼區塊;如果條件為假,則執行另一個程式碼區塊。if/else if/else– 依序檢查多個條件,並執行第一個條件為真的程式碼區塊。while– 當條件為真時重複執行一段程式碼。break– 立即退出目前迴圈。programStop– 立即結束專案。
wait#
在執行下一行程式碼之前,暫停專案一段時間。
Available Functionsvoid wait(
double time,
timeUnits units );
範圍 |
類型 |
描述 |
|---|---|---|
|
|
等待時間,以正數 |
|
|
時間單位: |
此函數不傳回值。
Examples// Stop driving after 1 second
Drivetrain.drive(forward);
wait(1, seconds);
Drivetrain.stop();
for#
重複執行一段程式碼。計數型執行指定次數;範圍型執行針對集合中的每個元素執行一次。
Syntax1 — 計數循環 — 運轉固定次數。
for (int i = 0; i < count; i++) { // code block }
Parameters2 — 基於範圍的循環 — 對集合中的每個項目執行一次。
for (type element : collection) { // code block }
範圍 |
類型 |
描述 |
|---|---|---|
|
|
程式碼區塊重複執行的次數。 |
|
類型名稱 |
集合中每個項目的資料型態(例如 |
|
|
用於保存循環目前項值的變數名稱。 |
|
|
儲存待循環遍歷元素的陣列(或向量)。 |
此控制結構不傳回值。
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則執行所附程式碼區塊。
if (condition) {
// code block
}
範圍 |
類型 |
描述 |
|---|---|---|
|
|
語句運行時會計算的表達式或變數。如果其值為 |
此控制結構不傳回值。
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#
根據條件是 true 或 false ,決定要執行哪個封閉的程式碼區塊。
if (condition) {
// code block if true
}
else {
// code block if false
}
範圍 |
類型 |
描述 |
|---|---|---|
|
|
語句運行時會對其進行求值的表達式或變數。如果其值為 |
此控制結構不傳回值。
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 is |
此控制結構不傳回值。
Notesif運行其程式碼區塊,如果條件為true。else if僅當所有先前的條件均為假時才檢查附加條件false。可以使用多個else if語句。else僅當前面的條件都不為真時才運行其程式碼區塊true。
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 ,就重複執行程式碼。
while (condition) {
// code block
}
範圍 |
類型 |
描述 |
|---|---|---|
|
|
每次迭代前都要進行求值的表達式或變數。如果值為 |
此控制結構不傳回值。
Notes它也可以像「等待直到」一樣使用,只需在條件前加上
!即可,如下例所示。
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 表示退出最內層循環。
break;
此語句不接受任何參數。
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;
}
}
programStop#
立即結束專案。
SyntaxBrain.programStop();
此語句不接受任何參數。
Return Values此語句不傳回值。
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();
}
}