コントロール#
導入#
制御機能には、タイミング、プログラムの流れ、条件分岐、およびプロジェクト終了のための機能が含まれています。これらの制御機能を使用すると、実行の一時停止、ループの作成、ロジックパスの定義、およびプログラムの終了を行うことができます。
機能とキーワード#
以下に、API関数やC++キーワードを含む、利用可能なコントロールの一覧を示します。
wait– プロジェクトを一定時間一時停止します。for– コードブロックを指定された回数繰り返します。if– 条件が真の場合にコードブロックを実行します。if/else– 条件が真の場合は 1 つのコードブロックを実行し、偽の場合は別のブロックを実行します。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#
コードブロックを繰り返します。カウント形式は指定された回数だけ実行され、範囲指定形式はコレクション内の各項目に対して1回ずつ実行されます。
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
}
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
ステートメントの実行時に評価される式または変数。最初に |
この制御構造は値を返しません。
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
}
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
各反復処理の前に評価される式または変数。0 |
この制御構造は値を返しません。
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();
}
}