switch문

package org.opentutorials.javatutorials.condition;
public class SwitchDemo {
	public static void main(String[] args) {
    
    	System.out.println("switch(1)");
        switch(1){
            case 1:
            	System.out.println("one");
            case 2:
            	System.out.println("two");
            case 3:
            	System.out.println("three");
            }
            
        System.out.println("switch(2)");
        switch(2){
            case 1:
            	System.out.println("one");
            case 2:
            	System.out.println("two");
            case 3:
            	System.out.println("three");
            }
        System.out.println("switch(3)");
        switch(3){
            case 1:
            	System.out.println("one");
            case 2:
            	System.out.println("two");
            case 3:
            	System.out.println("three");
            }
        }
    }

결과는 아래와 같다.

1

2

3

4

5

6

7

8

9

switch(1)

one

two

three

switch(2)

two

three

switch(3)

three

break

break를 만나면 switch 문의 실행이 즉시 중지된다. 

package org.opentutorials.javatutorials.condition;
public class SwitchDemo {
	public static void main(String[] args) {
    
    	System.out.println("switch(1)");
        switch(1){
            case 1:
            	System.out.println("one");
                break;
            case 2:
            	System.out.println("two");
                break;
            case 3:
            	System.out.println("three");
                break;
            }
            
        System.out.println("switch(2)");
        switch(2){
            case 1:
            	System.out.println("one");
                break;
            case 2:
            	System.out.println("two");
                break;
            case 3:
            	System.out.println("three");
                break;
            }
        System.out.println("switch(3)");
        switch(3){
            case 1:
            	System.out.println("one");
                break;
            case 2:
            	System.out.println("two");
                break;
            case 3:
            	System.out.println("three");
                break;
            }
        }
    }

따라서 위의 코드는 아래와 같이 if문으로 변경 할 수 있다.

package org.opentutorials.javatutorials.condition;
 
public class SwitchDemo2 {
 
    public static void main(String[] args) {
         
        int val = 1;
        if(val == 1){
            System.out.println("one");
        } else if(val == 2){
            System.out.println("two");
        } else if(val == 2){
            System.out.println("three");
        }
 
    }
 
}

 

default

package org.opentutorials.javatutorials.condition;
 
public class SwitchDemo {
 
    public static void main(String[] args) {
         
        System.out.println("switch(1)");
        switch(1){
        case 1:
            System.out.println("one");
            break;
        case 2:
            System.out.println("two");
            break;
        case 3:
            System.out.println("three");
            break;
        default:
            System.out.println("default");
            break;
        }
         
        System.out.println("switch(2)");
        switch(2){
        case 1:
            System.out.println("one");
            break;
        case 2:
            System.out.println("two");
            break;
        case 3:
            System.out.println("three");
            break;
        default:
            System.out.println("default");
            break;
        }
         
        System.out.println("switch(3)");
        switch(3){
        case 1:
            System.out.println("one");
            break;
        case 2:
            System.out.println("two");
            break;
        case 3:
            System.out.println("three");
            break;
        default:
            System.out.println("default");
            break;
        }
         
        System.out.println("switch(4)");
        switch(4){
        case 1:
            System.out.println("one");
            break;
        case 2:
            System.out.println("two");
            break;
        case 3:
            System.out.println("three");
            break;
        default:
            System.out.println("default");
            break;
        }
 
    }
 
}

위의 코드는 각 switch 문에 default:가 이끄는 구문을 추가했다. 그 결과는 아래와 같다.

1

2

3

4

5

6

7

8

switch(1)

one

switch(2)

two

switch(3)

three

switch(4)

default

가장 마지막은 default로 끝난다. 즉 주어진 케이스가 없는 경우 default 문이 실행된다는 것을 알 수 있다. 

 

switch 문을 사용할 때 한가지 주의 할 것은 switch의 조건으로는 몇가지 제한된 데이터 타입만을 사용할 수 있다.

byte, short, char, int, enum, String, Character, Byte, Short, Integer

'📌 java > java' 카테고리의 다른 글

java - while문, for문 (2020/02/07)  (0) 2020.02.07
java - 논리연산자 (2020/02/07)  (0) 2020.02.07
java - if문 (2029/02/05)  (0) 2020.02.05
java - 비교와 Boolean (2020/02/05)  (0) 2020.02.05
java - 전위,후위 연산자 (2020/02/04)  (0) 2020.02.04
복사했습니다!