do... while

설명

do...while 루프는 조건이 루프 끝에서 테스트되는 것을 제외하면 while 루프와 같은 방법으로 동작하므로, 루프는 적어도 한번 돈다.

문법

int x = 0; do { // 문 블럭 } while (condition);

conditiontrue 또는 false 를 계산하는 부울 식이다.

예제 코드

예제 코드 1

void setup() { Serial.begin(9600); Serial.println("====== TEST START ======"); int i = 0; do { Serial.print("Inside the DO WHILE loop: i = "); Serial.println(i); i++; // increase i by 1 } while (i < 5); Serial.println("====== TEST END ========"); } void loop() { }

시리얼 모니터에 결과:

COM6
Send
====== TEST START ====== Inside the DO WHILE loop: i = 0 Inside the DO WHILE loop: i = 1 Inside the DO WHILE loop: i = 2 Inside the DO WHILE loop: i = 3 Inside the DO WHILE loop: i = 4 ====== TEST END ========
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

예제 코드 2

int x = 0; do { delay(50); // 센서 안정화를 위해 기다림 x = readSensors(); // 센서 확인 } while (x < 100);

do...while loop vs while loop

The while loop checks the condition before executing the block of code; conversely, the do while loop checks the condition after executing the block of code. Therefore, the do while loop will always be executed at least once, even if the condition is false at the beginning.

The do...while and while loop are the same, except for the case in which the condition is false at the beginning.

For example:

  • Code with do...while loop
void setup() { Serial.begin(9600); Serial.println("====== TEST START ======"); int i = 10; do { Serial.print("Inside the DO WHILE loop: i = "); Serial.println(i); i++; // increase i by 1 } while (i < 5); Serial.println("====== TEST END ========"); } void loop() { }
COM6
Send
====== TEST START ====== Inside the DO WHILE loop: i = 10 ====== TEST END ========
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Code with while loop
void setup() { Serial.begin(9600); Serial.println("====== TEST START ======"); int i = 10; while (i < 5) { Serial.print("Inside the DO WHILE loop: i = "); Serial.println(i); i++; // increase i by 1 } Serial.println("====== TEST END ========"); } void loop() { }
COM6
Send
====== TEST START ====== ====== TEST END ========
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ 주의 및 경고:

There are three ways to escape the do while loop:

  • The condition of the do while loop becomes false.
  • The execution of the code reaches a break statement inside the loop.
  • The execution of the code reaches a goto statement inside the loop, which jumps to a label located outside of the loop.

더보기

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

※ OUR MESSAGES

  • We are AVAILABLE for HIRE. See how to hire us to build your project
  • Any suggestion, correction, and translation? please email us at ArduinoGetStarted@gmail.com, We appreciate it
  • We mainly keep improving the references in English. See English version of this page for the latest update.