출처: https://bumcrush.tistory.com/182 [맑음때때로 여름]
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
52
53
54
55
56
57
58
 
        
    //이중for문 탈출예제
        
        for(int i=0; i<10; i++) {
            
            System.out.println("i = "+i);
        
            for(int j =0; j<5 ; j++){
                System.out.println("\tj = " + j);
                if(i==5 && j==3) {
                    break;  
                    //for문 1개에 break 1개 i=5 j=3에 탈출하지만 다시 i=6부터 돌아간다.
                }
            }
        }
            
    // 이중for문 탈출 방법 1
            
            boolean b = false;
            
            for(int i=0; i<10; i++) {
                
                System.out.println("i = "+i);
            
                for(int j =0; j<5 ; j++){
                    System.out.println("\tj = " + j);
                    if(i==5 && j==3) {
                        b = true;
                    }
                    if(b == true) {
                        break;
                    }
                    
                }
                if(b == true) {
                    break;
    }
 
    }
            
            
        //  이중for문 탈출 방법 2
            
            outside:for(int i=0; i<10; i++) {   // outside를 앞에도 적어줌 ~:
                
                System.out.println("i = "+i);
            
                for(int j =0; j<5 ; j++){
                    System.out.println("\tj = " + j);
                    if(i==3 && j==2) {
                        break outside; // 단어는 outside가 아니라 아무거나 가능  
                        
                    }
                }
            }
            
            
cs

+ Recent posts