while

설명

while 루프는 계속, 무한히 돌아가며, 괄호 안의 식이 거짓이 될 때까지 돈다. 무엇인가가 테스트 변수를 바꾸어야 하며, 아니면 while 루프는 끝나지 않는다. 여러분의 코드에서 이것은 증가변수 또는 센서 테스트 같은 외부 조건일 수 있다.

문법

while(condition) { // 문 }

conditiontrue 또는 false 으로 평가되는 부울 식이다.

예제 코드

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

In the example above, the code in the loop will run, over and over again, as long as a variable (i) is less than 5.

시리얼 모니터에 결과:

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

※ 주의 및 경고:

  • When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop.
  • The following while loop loops forever:
while (true) { // statement(s) }
  • There are three ways to escape the while loop:
    • The condition of the 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 Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

※ OUR MESSAGES