continue 문은 루프(for, while, 또는 do...while)의 현재 반복의 나머지를 건너뛴다. 루프의 조건식을 체크하고 다른 일련의 반복을 진행한다.
예제 코드
Example with For Loop
Example 1
The following code exits the for loop when the i greater than 3
voidsetup() {Serial.begin(9600);Serial.println("====== TEST START ======");for (int i = 1; i < 5; i++) {Serial.print("Inside the FOR loop: i = ");Serial.print(i);Serial.println(", start");if(i > 2)continue;Serial.print("Inside the FOR loop: i = ");Serial.print(i);Serial.println(", end"); }Serial.println("====== TEST END ========");}voidloop() {}
시리얼 모니터에 결과:
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
====== TEST START ======
Inside the FOR loop: i = 1, start
Inside the FOR loop: i = 1, end
Inside the FOR loop: i = 2, start
Inside the FOR loop: i = 2, end
Inside the FOR loop: i = 3, start
Inside the FOR loop: i = 4, start
====== TEST END ========
Ln 11, Col 1
Arduino Uno on COM15
2
Example 2
아래 코드는 0에서 255까지의 코드 값을 PWMpin 에 쓰지만, 41에서 119 의 범위 값은 건너뛴다.
for (x = 0; x <= 255; x ++) {if (x > 40 && x < 120) { // 값 중 건너뛸 것을 만듦continue; }analogWrite(PWMpin, x);delay(50);}
Example with While Loop
The following code exits the while loop when the i greater than 3
voidsetup() {Serial.begin(9600);Serial.println("====== TEST START ======");int i = 0;while (i < 5) { i++; // increase i by 1Serial.print("Inside the WHILE loop: i = ");Serial.print(i);Serial.println(", start");if(i > 2)continue;Serial.print("Inside the WHILE loop: i = ");Serial.print(i);Serial.println(", end"); }Serial.println("====== TEST END ========");}voidloop() {}
시리얼 모니터에 결과:
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
====== TEST START ======
Inside the WHILE loop: i = 1, start
Inside the WHILE loop: i = 1, end
Inside the WHILE loop: i = 2, start
Inside the WHILE loop: i = 2, end
Inside the WHILE loop: i = 3, start
Inside the WHILE loop: i = 4, start
====== TEST END ========
Ln 11, Col 1
Arduino Uno on COM15
2
Example with Do While Loop
The following code exits the while loop when the i greater than 3
voidsetup() {Serial.begin(9600);Serial.println("====== TEST START ======");int i = 0;do { i++; // increase i by 1Serial.print("Inside the DO WHILE loop: i = ");Serial.print(i);Serial.println(", start");if(i > 2)continue;Serial.print("Inside the WHILE loop: i = ");Serial.print(i);Serial.println(", end"); } while (i < 5);Serial.println("====== TEST END ========");}voidloop() {}
시리얼 모니터에 결과:
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
====== TEST START ======
Inside the DO WHILE loop: i = 1, start
Inside the DO WHILE loop: i = 1, end
Inside the DO WHILE loop: i = 2, start
Inside the DO WHILE loop: i = 2, end
Inside the DO WHILE loop: i = 3, start
Inside the DO WHILE loop: i = 4, start
====== TEST END ========
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you. Additionally, some links direct to products from our own brand, DIYables .