본문 바로가기

[TIL] break 문, continue 문

인포꿀팁 발행일 : 2020-07-28

break 문

반복문 while, do while, for 이나 switch 문 내의 블록으로 제어를 빠져나오기 위해 사용된다.

 

 

 

break  라벨명;

while, do-while, for 만 가능 (swtich는 x)

break문 다음의 라벨명은 반드시 메소드내의 같은 유효범위를 갖는 라벨명만 가능하다.

 

 

continue문

반복문 while, do-while, for 에서 사용되며 continue를 만나면 루프에서 하나의 반복을 중단하고 루프의 다음 반복을 계속함

while문에서는 continue를 만나면 조건식으로 / for문은 증감식으로 올라간다.

continue 라벨명;

반복문이 다중으로 작성된 경우 한번에 두개 이상의 반복문 조건식으로 제어를 옮기기 위해 사용된다.

1) switch문 break 문 예시

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package ex0728;
 
import java.util.Scanner;
 
public class Break_Ex1 {
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num, s, ch;
        int start, offset;
 
        while (true) {
            do {
                System.out.print("1.합 2.짝수합 3.홀수합 4.종료");
                ch = sc.nextInt();
            } while (ch < 1 || ch > 4);
 
            if (ch == 4) {
                break// while(true)를 빠져나감
            }
            do {
                System.out.println("수?");
                num = sc.nextInt();
            } while (num < 1);
 
            s = 0;
            switch (ch) {
            case 1:
                start = 1;
                offset = 1;
                break;
            case 2:
                start = 2;
                offset = 2;
                break;
            default:
                start = 1;
                offset = 2;
                break;
            }
            for (int i = start; i <= num; i += offset) {
                s += i;
            }
 
            System.out.println("결과: " + s);
        }
        sc.close();
    }
 
}
 
cs

 

2) break 라벨; 예시

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package ex0728;
 
public class Break_Ex3 {
 
    public static void main(String[] args) {
/*
        for(int i =1;i<=3;i++) {
            for(int j=1;j<=3;j++) {
                if(i+j==4) {
                    break;
                }
                System.out.println("i:"+i+",j:"+j);
            }
        }
출력
i:1,j:1
i:1,j:2
i:2,j:1
 
*/
        go:
        for(int i =1;i<=3;i++) {
            for(int j=1;j<=3;j++) {
                if(i+j==4) {
                    break go;
                }
                System.out.println("i:"+i+",j:"+j);
            }
        }
/*
i:1,j:1
i:1,j:2
*/
    }
 
}
 
cs

 

3) continue문

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package ex0728;
 
import java.util.Scanner;
 
public class Continue_Ex1 {
// 10개의 수를 입력 받아 입력 받은 수의 합 구하기
    // 단 입력 받은수가 0이면 합에 누적하지 않는다.
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n, s;
        
        s=0;
        
        System.out.print("10개 수 입력하시오");
        for(int i =1; i<=10; i++) {
            n=sc.nextInt();
            if(n<=0) {
                continue// 증감식으로 이동 //아래 연산은 안함
            }
            s+=n;
        }
        System.out.println("결과:   "+s);
        sc.close();
    }
 
}
 
cs

4) continue label 문 예시

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package ex0728;
 
public class ContinueLabel_Ex2 {
//2~100까지 수 수중 소수를 한줄에 5개씩
    public static void main(String[] args) {
        int cnt = 0;
        
        jump:
            for(int n=2;n<=100;n++) {
                for (int i =2;i<=(n/2);i++) {
                    if(n%i==0) { // 소수가 아니면
                        continue jump;
                    }
                }
                System.out.printf("%5d", n);
                cnt++;
                if(cnt%5==0) {
                    System.out.println();
                }
            }
 
    }
 
}
/*
2    3    5    7   11
13   17   19   23   29
31   37   41   43   47
53   59   61   67   71
73   79   83   89   97
*/
cs

 

 

 

 

 

'Language > JAVA' 카테고리의 다른 글

[TIL] 배열 (2)  (0) 2020.07.30
[TIL] 배열 Array  (0) 2020.07.29
[TIL] for문 예제  (0) 2020.07.28
[TIL] for문  (0) 2020.07.27
[TIL] do-while 문  (0) 2020.07.27

댓글