switch case

설명

if 문과 같이, switch case 는 다양한 조건에서 실행되어야 하는 다른 코드를 프로그래머가 지정하는 것을 허용하여, 프로그램의 흐름을 제어한다. 특히, switch 문은 변수 값을 case 문에 지정된 값과 비교한다. case 문이 그 값이 변수 값과 같은 것을 찾으면 해당 case 문에 있는 코드가 실행된다.

break 키워드는 switch 문을 빠져 나오고, 각 case의 끝에 전형적으로 쓰인다. break 문이 없으면, switch 문이 break 나올 때까지 (break 안 나오면 switch 문 끝날 때까지) 다음 문장을 계속 실행("falling-through")한다.

문법

switch (var) { case label1: // statements break; case label2: // statements break; default: // statements break; }

매개변수

  • var: 다양한 case 와 비교할 변수 값. 허용되는 자료형: int, char
  • label1, label2: 상수. 허용되는 자료형: int, char

반환값

  • 없음

예제 코드

void setup() { Serial.begin(9600); Serial.println("====== TEST START ======"); for (int i = 0; i < 5; i++) { switch (i) { case 0: Serial.print("Inside case 0: i = "); Serial.println(i); break; case 1: Serial.print("Inside case 1: i = "); Serial.println(i); break; case 2: Serial.print("Inside case 2: i = "); Serial.println(i); break; default: Serial.print("Inside default: i = "); Serial.println(i); break; } } Serial.println("====== TEST END ========"); } void loop() { }

시리얼 모니터에 결과:

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

※ 주의 및 경고:

if we does not use the break in a case, the next case will also be executed. The execution is stopped when it reaches a break. Let's comment or remove a break in the above example and see the result.

void setup() { Serial.begin(9600); Serial.println("====== TEST START ======"); for (int i = 0; i < 5; i++) { switch (i) { case 0: Serial.print("Inside case 0: i = "); Serial.println(i); break; case 1: Serial.print("Inside case 1: i = "); Serial.println(i); // break; case 2: Serial.print("Inside case 2: i = "); Serial.println(i); break; default: Serial.print("Inside default: i = "); Serial.println(i); break; } } Serial.println("====== TEST END ========"); } void loop() { }

시리얼 모니터에 결과:

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

As you can see, when i = 1, the code run through case 1 and case 2.

Advanced Usage

If we miss a break in a case by accident, it results in the wrong operation of the code. Why the language does not force to use break instead of optional? Let's see two advanced usages to know the reason why.

Advanced Usage 1

Let's see the below example:

void setup() { Serial.begin(9600); for (int i = 0; i < 5; i++) { switch (i) { case 0: Serial.println("HELLO!"); break; case 1: Serial.println("ARDUINO"); break; case 2: Serial.println("ARDUINO"); break; case 3: Serial.println("ARDUINO"); break; default: Serial.println("ArduinoGetStarted.com"); break; } } } void loop() { }

시리얼 모니터에 결과:

COM6
Send
HELLO! ARDUINO ARDUINO ARDUINO ArduinoGetStarted.com
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

In the above code, you can see the case 1, 2 and 3 have the same code (do the same work). Let's remove all code in the case 1 and case 2:

void setup() { Serial.begin(9600); for (int i = 0; i < 5; i++) { switch (i) { case 0: Serial.println("HELLO!"); break; case 1: case 2: case 3: Serial.println("ARDUINO"); break; default: Serial.println("ArduinoGetStarted.com"); break; } } } void loop() { }

시리얼 모니터에 결과:

COM6
Send
HELLO! ARDUINO ARDUINO ARDUINO ArduinoGetStarted.com
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

As you can see, the results of both example codes are the same. But the second example code are shorter. Furthermore, if you get familiar to the second example code, you will realize that the second code is more readable. That is the benefit of not using the break;

Advanced Usage 2

Let's see the below example:

void setup() { Serial.begin(9600); for (int i = 0; i < 4; i++) { switch (i) { case 0: Serial.println("Hello!"); break; case 1: Serial.print("Let's learn "); Serial.println("Arduino"); break; case 2: Serial.println("Arduino"); break; default: Serial.println("via ArduinoGetStarted.com"); break; } } } void loop() { }

시리얼 모니터에 결과:

COM6
Send
Hello! Let's learn Arduino Arduino via ArduinoGetStarted.com
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

In the above code, you can see the case 1 and 2 have the same a line of code (Serial.println("Arduino")). Let's compare with the below code:

void setup() { Serial.begin(9600); for (int i = 0; i < 4; i++) { switch (i) { case 0: Serial.println("Hello!"); break; case 1: Serial.print("Let's learn "); case 2: Serial.println("Arduino"); break; default: Serial.println("via ArduinoGetStarted.com"); break; } } } void loop() { }

시리얼 모니터에 결과:

COM6
Send
Hello! Let's learn Arduino Arduino via ArduinoGetStarted.com
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

As you can see, the results of both example codes are the same. However, the second example code are shorter. That is another benefit of not using the break;

더보기

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