ควบคุม#

การแนะนำ#

ส่วนควบคุมประกอบด้วยฟังก์ชันสำหรับการกำหนดเวลา การไหลของโปรแกรม ตรรกะแบบมีเงื่อนไข และการยุติโครงการ ส่วนควบคุมเหล่านี้ช่วยให้คุณสามารถหยุดการทำงานชั่วคราว สร้างลูป กำหนดเส้นทางตรรกะ และจบโปรแกรมได้

ฟังก์ชันและคำสำคัญ#

ด้านล่างนี้คือรายการของส่วนควบคุมที่มีให้ใช้งาน รวมถึงฟังก์ชัน 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();
  }
}