goto

설명

프로그램 흐름을 프로그램에서 가리키는 라벨로 보낸다

문법

label: goto label; // 프로그램 흐름을 label 로 보냄

예제 코드

예제 코드 1

void setup() { Serial.begin(9600); Serial.println("SETUP - TOP"); goto BOTTOM; Serial.println("SETUP - MIDDLE"); BOTTOM: Serial.println("SETUP - BOTTOM"); } void loop() { }

시리얼 모니터에 결과:

Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
SETUP - TOP SETUP - BOTTOM
Ln 11, Col 1
Arduino Uno on COM15
2

As we can see, the text "SETUP - MIDDLE" is not printed.

예제 코드 2

for (byte r = 0; r < 255; r++) { for (byte g = 255; g > 0; g--) { for (byte b = 0; b < 255; b++) { if (analogRead(0) > 250) { goto bailout; } // more statements ... } } } bailout: // more statements ...

※ 주의 및 경고:

C++ 프로그램에서 goto 사용은 권장하지 않고, C 프로그램 책의 어떤 저자는 goto 이 절대 필요하지 않다고 하지만, 신중하게 쓰이면, 어떤 프로그램을 간단하게 할 수 있다. 많은 프로그래머가 goto 사용을 싫어하는 까닭은 goto 문 사용이 제한되지 않으면, 정의되지 않는 프로그램 흐름을 만들기 쉬운데, 이러면 버그를 잡을 수 없다. 그렇긴 하지만, goto 문이 편리하고, 코딩을 간단하게 할 때도 있다. 그런 상황 중 하나는 깊게 중첩된 for 루프 또는 if 논리 블록에서 어떤 조건에서 빠져 나올 때다.

더보기

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
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 .

※ OUR MESSAGES