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

๋ฐ˜์‘ํ˜•

๋ฐ˜๋ณต๋ฌธ while


- while๋ฌธ์€ ์กฐ๊ฑด์‹์„ ๋งŒ์กฑํ•˜๋Š” ๋™์•ˆ ๋ฐ˜๋ณต๋ฌธ์„ ์‹คํ–‰ํ•˜๋Š”๋ฐ, ์กฐ๊ฑด์‹์„ ๊ฒ€์‚ฌํ•ด์„œ ์กฐ๊ฑด์‹์ด ์ฐธ์ด๋ฉด ์‹คํ–‰๋ฌธ์„ ๋ฐ˜๋ณตํ•˜๊ณ  ๊ฑฐ์ง“์ด๋ฉด while๋ฌธ์„ ๋น ์ ธ๋‚˜์˜จ๋‹ค.

- while๋ฌธ์— ๋“ค์–ด๊ฐ€๋Š” ์กฐ๊ฑด์‹๋„ if๋ฌธ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋„ ๋ฐ˜๋“œ์‹œ ๋ถ€์šธํ˜•ํƒ€์ž…์œผ๋กœ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผํ•œ๋‹ค.

- ์กฐ๊ฑด์‹ ์•ˆ์— true๋ฅผ ๋„ฃ์œผ๋ฉด ๋ฌดํ•œ๋ฃจํ”„๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜๋“œ์‹œ ํƒˆ์ถœ๊ตฌ๋ฌธ์„ ๋„ฃ์–ด์•ผํ•œ๋‹ค.

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
 
public class WhileEx1 {
 
    public static void main(String[] args) {
        /*
        int i = 1; //์ œ์–ด๋ณ€์ˆ˜
        
        while(i <= 10) {
            
            System.out.println(i);
            
            i++; //์ œ์–ด๋ณ€์ˆ˜ ์กฐ์ž‘์„ ํ†ตํ•ด ๋ฐ˜๋ณต๋ฌธ์˜ ์กฐ๊ฑด์‹์ด ์–ธ์  ๊ฐ€ false๊ฐ€ ๋‚˜์˜ค๊ฒŒ ํ•ด์•ผํ•จ
        }
        */
        
        
    
        //1~10๊นŒ์ง€ ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•˜๋Š” ๋กœ์ง.
        int i = 1;
        int sum = 0;
        
        while(i <= 10) {
            
            sum = sum + i;
            i++;
        }
        
        System.out.println("1~10๊นŒ์ง€ ํ•ฉ:" + sum);
        
    }
}
 
 
 
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
import java.util.Scanner;
 
public class WhileEx2 {
 
    public static void main(String[] args) {
        
        //์ •์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ ํ•ด๋‹น ์ •์ˆ˜๊ฐ€ ์†Œ์ˆ˜ ์ธ์ง€ ํŒ๋ณ„.
        
        Scanner scan = new Scanner(System.in);
        
        System.out.print("์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”>");
        int n = scan.nextInt();
        
        
        int i = 2//n์˜ ์†Œ์ˆ˜ ํŒ๋ณ„์„ ์œ„ํ•ด ๋‚˜๋ˆ„์–ด๋ณผ ๋ณ€์ˆ˜.
        while( n % i != 0 ) {
            
            i++;
        }
        
        
        
        if(n == i) {
            System.out.println(n + "์€ ์†Œ์ˆ˜์ž…๋‹ˆ๋‹ค.");
        } else {
            System.out.println(n + "์€ ์†Œ์ˆ˜๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค");
        }
        
        
        
        
    }
}
 
 
 

 

 

๋ฐ˜๋ณต๋ฌธ do ~ while


- while๋ฌธ์€ ์กฐ๊ฑด์‹์„ ๋จผ์ € ๊ฒ€์‚ฌํ•˜๊ณ  ์‹คํ–‰๋ฌธ์ด ๋ฐ˜๋ณต๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ฒ˜์Œ ์‹คํ–‰ ์กฐ๊ฑด์ด false๋ผ๋ฉด ์‹คํ–‰๋ฌธ์ด ๋‹จ ํ•œ๋ฒˆ๋„ ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค.

- ๊ทธ๋Ÿฌ๋‚˜ do ~ while๋ฌธ์€ do์ดํ•˜์˜ ๊ตฌ๋ฌธ์ด ๋จผ์ € ํ•œ ๋ฒˆ ์‹คํ–‰๋œ ๋’ค์— ์กฐ๊ฑด์‹์„ ๊ฒ€์‚ฌํ•˜๋ฏ€๋กœ ๊ฒฐ๊ณผ๊ฐ€ true์ด๋“  false์ด๋“  ๋ฌด์กฐ๊ฑด ํ•œ ๋ฒˆ์€ ์‹คํ–‰์ด ๋œ๋‹ค.

- do ~ while๋ฌธ์€ ์กฐ๊ฑด์‹์˜ ๊ฒฐ๊ณผ์— ์ƒ๊ด€์—†์ด ๋ฃจํ”„๋ฅผ ํ•œ ๋ฒˆ ์ด์ƒ ์‹คํ–‰์‹œํ‚ค๋„๋ก ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

 

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
import java.util.Scanner;
 
public class DowhileEx {
 
    public static void main(String[] args) {
        
        //1๋ถ€ํ„ฐ ์ž…๋ ฅ๋ฐ›์€ ์ˆ˜๊นŒ์ง€ ๋ˆ„์  ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•˜๋Š” ๋กœ์ง
        Scanner scan = new Scanner(System.in);
        System.out.print(">");
        int target = scan.nextInt();
        
        int i = 1//์ œ์–ด๋ณ€์ˆ˜
        int sum1 = 0//ํ•ฉ๊ณ„๋ณ€์ˆ˜
        int sum2 = 0;
        
        do {
            sum1 += i;
            i++;
        } while(i <= target);
        System.out.println("1๋ถ€ํ„ฐ ์ž…๋ ฅํ•œ ์ˆ˜ ๊นŒ์ง€์˜ ๋ˆ„์ ํ•ฉ๊ณ„ : " + sum1);
        
        i = 1;
        
        while(i <= target) {
            sum2 += i;
            i++;            
        }
        System.out.println("1๋ถ€ํ„ฐ ์ž…๋ ฅํ•œ ์ˆ˜ ๊นŒ์ง€์˜ ๋ˆ„์ ํ•ฉ๊ณ„ : " + sum2);
        
        
    }
}
 
 
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€