ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐ˜์‘ํ˜•

ํƒˆ์ถœ๋ฌธ


- ํƒˆ์ถœ๋ฌธ์€ ํ”„๋กœ๊ทธ๋žจ์˜ ์‹คํ–‰ ์ˆœ์„œ๋ฅผ ์ง€์ •๋œ ๊ณณ์œผ๋กœ ์ด๋™์‹œํ‚ค๋Š” ๋ฌธ์žฅ์œผ๋กœ ๋ฐ˜๋ณต๋ฌธ ๋˜๋Š” ๋ธ”๋ก๊ตฌ์กฐ์—์„œ ๋น ์ ธ ๋‚˜์˜ฌ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
- ํƒˆ์ถœ๋ฌธ์˜ ํ‚ค์›Œ๋“œ๋Š” break, continue, return๋ฌธ์ด ์žˆ๋‹ค.
- goto๋Š” ์ž๋ฐ”์˜ ํ‚ค์›Œ๋“œ์—๋Š” ํฌํ•จ๋˜์–ด ์žˆ์œผ๋‚˜ ์‚ฌ์šฉํ•˜์ง€๋Š” ์•Š๋Š”๋‹ค.

 

 

 

 

 

1. break


- break๋Š” ๋ฐ˜๋ณต๋ฌธ ๋‚ด์˜ ์–ด๋–ค ๊ณณ์ด๋“  ์œ„์น˜ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ฒ˜๋ฆฌ ๋„์ค‘์— break๋ฅผ ๋งŒ๋‚˜๋ฉด ๋ฐ˜๋ณต๋ฌธ์„ ๊ณง๋ฐ”๋กœ ๋น ์ ธ๋‚˜์˜จ๋‹ค.

- ์ค‘์ฒฉ ๋ฐ˜๋ณต๋ฌธ์˜ ๊ฒฝ์šฐ์— ๋‚ด๋ถ€ ๋ฐ˜๋ณต๋ฌธ์—์„œ break๊ฐ€ ์‚ฌ์šฉ๋˜์—ˆ์„ ๊ฒฝ์šฐ ๊ทธ break๋ฌธ์ด ์†ํ•œ ๋‚ด๋ถ€ ๋ฐ˜๋ณต๋ฌธ๋งŒ ๋น ์ ธ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค.

- ์ค‘์ฒฉ ๋ฐ˜๋ณต๋ฌธ์—์„œ ๋ฐ”๊นฅ์ชฝ ๋ฐ˜๋ณต๋ฌธ๊นŒ์ง€ ์ข…๋ฃŒ์‹œํ‚ค๋ ค๋ฉด ๋ฐ”๊นฅ์ชฝ ๋ฐ˜๋ณต๋ฌธ์— ์ด๋ฆ„(label) ์„ ๋ถ™์ด๊ณ , "break ์ด๋ฆ„;"์„ ์‚ฌ์šฉ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
public class BreakEx1 {
 
    public static void main(String[] args) {
        
        /*
         * ๋ฌดํ•œ๋ฃจํ”„
         * -๋ฐ˜๋ณต๋ฌธ์˜ ํšŸ์ˆ˜๋ฅผ ๋ชจ๋ฅผ ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋ฉฐ, ํŠน์ • ์กฐ๊ฑด์—์„œ ๋ฐ˜๋ณต๋ฌธ์„ ์ข…๋ฃŒ์‹œํ‚ค๋Š” ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
         * -์ž๋ฐ”์—์„œ ๋ฌดํ•œ๋ฃจํ”„๋Š” ์ฃผ๋กœ while๋ฌธ์„ ์‚ฌ์šฉํ•œ๋‹ค.
         */
        
        //while๋ฌธ์—์„œ๋Š” ๋น ์ ธ๋‚˜์˜ค๊ฒŒํ•  ์ œ์–ด๋ณ€์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค.
        int i = 0;
        while(true) {
            System.out.println(i);
            if(i==10) {
                break;
            }
            i++;
        }
    }
}
 
 
 
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
import java.util.Scanner;
 
public class BreakEx2 {
 
    public static void main(String[] args) {
        
        while(true) {
            Scanner scan = new Scanner(System.in);
            
            System.out.println("--------------");
            System.out.println("15 X 6 = ?");
            System.out.print(">");
            int num = scan.nextInt();
            
            if(num == 80) {
                System.out.println("์ •๋‹ต์ž…๋‹ˆ๋‹ค!");
                break//if๋ฌธ์„ ๋น ์ ธ๋‚˜์˜ค๋Š”๊ฒŒ ์•„๋‹ˆ๊ณ  ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด while๋ฌธ์„ ๋น ์ ธ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค.
                //๋น ์ ธ๋‚˜์˜ฌ ์œ„์น˜๋ฅผ ์ž˜ ๊ฒฐ์ •ํ•ด์•ผํ•จ
            }
            
            System.out.println("ํ‹€๋ ธ์–ด!");
        }
    }
}
 
 
 
 
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
 
public class BreakEx3 { //์ด์ค‘ํฌ๋ฌธ์—์„œ์˜ break
 
    public static void main(String[] args) {
        
        for(int i = 0; i<3; i++) {
            
            for(int j = 0; j<2; j++) {
                
                if(i==j) {
                    break//๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๋ฐ˜๋ณต๋ฌธ ๋น ์ ธ๋‚˜์˜ด
                }
                System.out.println(i + " - " + j);
            }
        }
        
        
        /*
         * (์ค‘์ฒฉ ๋ฐ˜๋ณต๋ฌธ์„ ๋น ์ ธ๋‚˜์˜ค๊ธฐ ์œ„ํ•ด์„œ๋Š”)
         * ๋‚ด๋ถ€ ๋ฐ˜๋ณต๋ฌธ์—์„œ break๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์™ธ๋ถ€ ๋ฐ˜๋ณต๋ฌธ๊นŒ์ง€ ํƒˆ์ถœ์‹œํ‚ค๋ ค๋ฉด 
         * ํƒˆ์ถœ์‹œํ‚ฌ ๋ฐ˜๋ณต๋ฌธ์— ์ด๋ฆ„์„ ๋ถ™์ด๊ณ  break ์ด๋ฆ„; ์„ ์‚ฌ์šฉํ•ด์„œ ๋น ์ ธ๋‚˜์˜จ๋‹ค.
         * -> ๋ผ๋ฒจ์‚ฌ์šฉ
         */
        
        //charํ˜•๋„ ๋ฐ˜๋ณต๋ฌธ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. (์œ ๋‹ˆ์ฝ”๋“œ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ)
        //A์˜ ์œ ๋‹ˆ์ฝ”๋“œ ๊ฐ’์€ 65, Z์˜ ์œ ๋‹ˆ์ฝ”๋“œ ๊ฐ’์€ 90์ด๋‹ค.
        banana: for(char upper = 'A'; upper <= 'Z'; upper++) {
            
            for(char lower = 'a'; lower<='z'; lower++) {
                
                System.out.println(upper + " - " + lower); //๋ฌธ์ž๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค
                if( lower == 'f' ) {
                    break banana; 
                }
            }
        }
    }
}
 
 
 
 
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
import java.util.Scanner;
 
public class BreakQuiz1 {
 
    public static void main(String[] args) {
        
        Scanner scan = new Scanner(System.in);
        
        int comAnser; //์ปดํ“จํ„ฐ ์ •๋‹ต
        int userAnser; //์‚ฌ์šฉ์ž ์ •๋‹ต
        
        int Acount = 0//์ •๋‹ตํšŸ์ˆ˜
        int Rcount = 0//์˜ค๋‹ตํšŸ์ˆ˜
        
        while(true) {
            //1๋ถ€ํ„ฐ 100๊นŒ์ง€์˜ ๋žœ๋ค๊ฐ’ ๋‘ ๊ฐœ
            int num1 = (int)(Math.random()*100)+1;
            int num2 = (int)(Math.random()*100)+1;
            
            comAnser = num1 + num2; //์ •๋‹ต๊ฐ’ ์ €์žฅ
            
            System.out.println("----------------------------");
            System.out.println(num1 + " + " + num2 + " = " + "?");
            System.out.println("[๋ฌธ์ œ๋ฅผ ๊ทธ๋งŒ ํ‘ธ์‹œ๋ ค๋ฉด 0์„ ์ž…๋ ฅํ•˜์„ธ์š”.]");
            System.out.print("> ");
            userAnser = scan.nextInt();
            
            //break๋ฌธ์„ ์œ„์ชฝ์œผ๋กœ ๋‘์–ด์•ผํ•œ๋‹ค.
            if(userAnser == 0) {
                System.out.println("ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.");
                System.out.println("-------------------");
                break;
            }
            
            if(comAnser == userAnser) {
                System.out.println("์ •๋‹ต์ž…๋‹ˆ๋‹ค!");
                Acount++;
            } else {
                System.out.println("ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค.");
                Rcount++;
            }
        }
        System.out.println("์ •๋‹ตํšŸ์ˆ˜ : " + Acount + "ํšŒ");
        System.out.println("์˜ค๋‹ตํšŸ์ˆ˜ : " + Rcount + "ํšŒ");
        
        scan.close();
    }
}
 
 
 

 

 

 

 

 

2. continue


- continue๋ฌธ์€ ๋ฐ˜๋ณต๋ฌธ ๋‚ด์˜ ์–ด๋–ค ๊ณณ์ด๋“ ์ง€ ์œ„์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

- continue๋ฌธ์„ ๋งŒ๋‚˜๋ฉด ๋ฐ˜๋ณต๋ฌธ์˜ ๋งจ ์ฒ˜์Œ์œผ๋กœ ์ œ์–ด๊ฐ€ ์ด๋™ํ•˜์—ฌ ๋‹ค์Œ ๋ฐ˜๋ณต๋ฌธ์„ ์œ„ํ•œ ์ฆ/๊ฐ์‹์„ ์‹คํ–‰ํ•œ๋‹ค.

- 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
 
public class ContinueEx1 {
 
    public static void main(String[] args) {
        
        
        //continue๊ฐ€ for๋ฌธ์—์„œ๋Š” ์ฆ๊ฐ์‹์œผ๋กœ ์˜ฌ๋ผ๊ฐ
        for(int i = 1; i<=10; i++) {
            
            if(i == 5) { //5๊ฐ€๋˜์—ˆ์„๋•Œ ๋ฐ”๋กœ ์œ„๋กœ ์˜ฌ๋ผ๊ฐ”๊ธฐ ๋•Œ๋ฌธ์— ์ถœ๋ ฅ์—์„œ 5๊ฐ€ ์•ˆ๋ณด์ž„
                continue;
            }
            System.out.println(i);
        }
        
        for(int i = 1; i<=10; i++) {
            
            if(i % 2 == 0) { //์ง์ˆ˜๋ฅผ ๋งŒ๋‚˜๋ฉด i๊ฐ’์„ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค
                continue;
            }
            System.out.println(i); //๋”ฐ๋ผ์„œ ํ™€์ˆ˜๋งŒ ์ถœ๋ ฅ์ด ๋จ
        }
    }
}
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
public class ContinueEx2 {
 
    public static void main(String[] args) {
        
        //continue๊ฐ€ while๋ฌธ์—์„œ๋Š” ์กฐ๊ฑด์‹์œผ๋กœ ์˜ฌ๋ผ๊ฐ
        //while๋ฌธ์—๋Š” ์ œ์–ด๋ณ€์ˆ˜, ์ œ์–ด๋ณ€์ˆ˜์˜ ์ฆ๊ฐ์‹, ์กฐ๊ฑด ์„ธ๊ฐ€์ง€๊ฐ€ ํ•„์š”ํ•˜๋‹ค
        int i = 0;
        while(i <= 10) {
        
            if(i%2 == 1) {
                System.out.println("continue๋ฅผ ๋งŒ๋‚˜์„œ ์ด๋™์ด ์กฐ๊ฑด์œผ๋กœ ๊ฐ‘๋‹ˆ๋‹ค.");
                i++;
                continue//while์˜ ์กฐ๊ฑด์œผ๋กœ ๊ฐ„๋‹ค
            }
            System.out.println(i);
            i++;
        }
    }
}
 
 

 

 


3. return


- return๋ฌธ์€ ๋ฉ”์„œ๋“œ์˜ ์‹คํ–‰์„ ๋๋‚ด๊ณ  ๊ฒฐ๊ณผ ๊ฐ’์„ ํ˜ธ์ถœ๋œ ๋ถ€๋ถ„์— ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์žฅ์ด๋‹ค.

- return๋ฌธ๋งŒ ๋‹จ๋…์œผ๋กœ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ๋Š” ํ˜ธ์ถœ๋œ ๊ณณ์œผ๋กœ ์ œ์–ด๋งŒ ๋„˜์–ด๊ฐ€์ง€๋งŒ, return๋ฌธ ๋’ค์— ์—ฐ์‚ฐ์‹๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜๋ฉด ์‹์˜ ๊ฒฐ๊ณผ ๊ฐ’๊ณผ ์ œ์–ด๊ฐ€ ํ•จ๊ป˜ ๋„˜์–ด๊ฐ„๋‹ค.

 

 

 

 

 

๋ฐฐ์—ด๊ณผ ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•œ ๋ฌธ์ œ

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
import java.util.Scanner;
 
public class ArrayInsertEx {
 
    public static void main(String[] args) {
        
        //๋ฐฐ์—ด์„ ์ž˜ ๋‹ค๋ค„์•ผํ•œ๋‹ค.
        
        Scanner scan = new Scanner(System.in); //์Šค์บ๋„ˆ ์ƒ์„ฑ
        String[] foods = new String[100]; //๋ฐฐ์—ด ์ƒ์„ฑ
        
        System.out.println("# ๋จน๊ณ ์‹ถ์€ ์Œ์‹์„ ์ž…๋ ฅํ•˜์„ธ์š”!!");
        System.out.println("# ์ž…๋ ฅ์„ ์ค‘์ง€ํ•˜๋ ค๋ฉด <๊ทธ๋งŒ>์ด๋ผ๊ณ  ์ž…๋ ฅํ•˜์„ธ์š”.");
        
        int count = 0;
        while(true) { //๋ฌดํ•œ  ๋ฃจํ”„
            
            System.out.print("> ");
            String answer = scan.nextLine(); //๋ผ์ธ์€ ๊ณต๋ฐฑ๋„ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.
            
            if(answer.equals("๊ทธ๋งŒ")) { //๋ฌธ์ž์—ด์˜ ๋™๋“ฑ๋น„๊ต๋Š” equals๋ฅผ ์‚ฌ์šฉ
                System.out.println("์ž…๋ ฅ ์ข…๋ฃŒ!");
                break;
            }
            
            foods[count] = answer;
            count++;
        }
        
        
        System.out.print("๋‚ด๊ฐ€ ๋จน๊ณ ์‹ถ์€ ์Œ์‹ : ");
        
        System.out.print("[ ");
        for(int i = 0; i<count; i++) {
            System.out.print(foods[i] + " ");
        }
        System.out.print("]");
 
    }
}
 
 
 
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€