ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Java] ์กฐ๊ฑด๋ฌธ(if, else, else if), ๋ฐ˜๋ณต๋ฌธ(for, while)
    Android/Java 2021. 1. 15. 23:09
    ๋ฐ˜์‘ํ˜•
    • ์ด ๊ธ€์€ "์ž๋ฐ” ์˜จ๋ผ์ธ ์Šคํ„ฐ๋”” ๋‚ด์šฉ"์„ ๊ณต๋ถ€ํ•˜์—ฌ ์ž‘์„ฑํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

    ์กฐ๊ฑด๋ฌธ

    if, else if, else

    ํ•ด๋‹น ์กฐ๊ฑด๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ์กฐ๊ฑด์— ๋งž๋Š” ์›ํ•˜๋Š” ๋กœ์ง์„ ์‹คํ–‰์‹œํ‚ต๋‹ˆ๋‹ค.

    public class Main {
        public static void main(String[] args) {
    
            int num = 2;
    
            if(num % 2 == 0){
                System.out.println(num+"์€ ์ง์ˆ˜์ž…๋‹ˆ๋‹ค");
            }else{
                System.out.println(num+"์€ ํ™€์ˆ˜์ž…๋‹ˆ๋‹ค");
            }
    
    
            if(num % 3 == 0){
                System.out.println(num+"์„ 3์œผ๋กœ ๋‚˜๋ˆ„๋ฉด ๋‚˜๋จธ์ง€๋Š” 0์ž…๋‹ˆ๋‹ค");
            }else if(num % 3 == 1){
                System.out.println(num+"์„ 3์œผ๋กœ ๋‚˜๋ˆ„๋ฉด ๋‚˜๋จธ์ง€๋Š” 1์ž…๋‹ˆ๋‹ค");
            }else{
                System.out.println(num+"์„ 3์œผ๋กœ ๋‚˜๋ˆ„๋ฉด ๋‚˜๋จธ์ง€๋Š” 2์ž…๋‹ˆ๋‹ค");
            }
    
        }
    }

    switch-case

    switch๋ฌธ์€ ๋ณ€์ˆ˜์— ๋”ฐ๋ผ ์—ฌ๋Ÿฌ case๋กœ ๋‚˜๋ˆˆ ํ›„, case์— ๋งž๋Š” ๋กœ์ง์„ ์‹คํ–‰์‹œํ‚ต๋‹ˆ๋‹ค.

    public class Main {
        public static void main(String[] args) {
    
            int num = 2;
    
            switch (num){
                case 1:
                    System.out.println(1);
                    break;
                case 2:
                    System.out.println(2);
                    break;
                case 3:
                    System.out.println(3);
                    break;
                default:
                    System.out.println(-1);
            }
    
        }
    }

    ๋ฐ˜๋ณต๋ฌธ

    for

    • for(statement1 ; statement2 ; statement 3) ํ˜•ํƒœ๋กœ ๋‘์–ด ๊ฐ ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋ฐ˜๋ณต๋ฌธ์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
    • statement1 : ๋กœ์ง์ด ์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ์‹คํ–‰๋˜๋Š” ๊ตฌ๋ฌธ์ž…๋‹ˆ๋‹ค.
    • statement2 : ์กฐ๊ฑด์ด ์ฐธ์ผ ๊ฒฝ์šฐ๋งŒ ๋กœ์ง์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
    • statement3 : ๋กœ์ง์ด ๋‹ค ์‹คํ–‰๋œ ํ›„ ๋งˆ์ง€๋ง‰์œผ๋กœ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ์ดํ›„ ์กฐ๊ฑด์„ ํ™•์ธํ•˜์—ฌ ์ฐธ์ผ ๊ฒฝ์šฐ ๋‹ค์‹œ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.
    public class Main {
        public static void main(String[] args) {
    
            int[] arr = new int[]{1,2,3,4,5};
            int size = 5;
    
            for(int i=0;i<5;i++){
                System.out.println(arr[i]);
            }
    
        }
    }
    • int i = 0 ์œผ๋กœ ๋ณ€์ˆ˜ i๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ๋กœ์ง ์‹คํ–‰๋˜๊ธฐ ์ „์— ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.
    • ์ดํ›„ i<5 ์กฐ๊ฑด์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ์ฐธ์ผ ๊ฒฝ์šฐ ๋กœ์ง์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
    • ๋กœ์ง์ด ๋‹ค ์‹คํ–‰๋œ ํ›„ i++์„ ์‹คํ–‰ํ•œ ํ›„ ์กฐ๊ฑด์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ์กฐ๊ฑด์ด ์ฐธ์ผ ๊ฒฝ์šฐ ๋กœ์ง์„ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์กฐ๊ฑด์ด ๊ฑฐ์ง“์ด ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

    for each

    • ๋ฐฐ์—ด๊ณผ ๊ฐ™์ด ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” container์„ ํƒ์ƒ‰ํ•˜๊ธฐ ์œ ์šฉํ•œ ๋ฐ˜๋ณต๋ฌธ์ž…๋‹ˆ๋‹ค.
    • for ๊ตฌ๋ฌธ์—์„œ์˜ ์ฝ”๋“œ์™€ ๋‹ค๋ฅด๊ฒŒ ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผํ•˜์ง€ ์•Š์•„๋„ ๊ฐ’์„ ์‰ฝ๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
    public class Main {
        public static void main(String[] args) {
    
            int[] arr = new int[]{1,2,3,4,5};
            int size = 5;
    
            for(int elem : arr){
                System.out.println(elem);
            }
    
        }
    }

    while

    ์กฐ๊ฑด์ด ์ฐธ์ผ ๊ฒฝ์šฐ ๋กœ์ง์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

    public class Main {
        public static void main(String[] args) {
    
            int num = 5;
    
            while(num>0){
                System.out.println(num);
                num--;
            }
            // 5 4 3 2 1
    
            num = 5;
            while(num>0){
                num--;
                if(num%2 == 0){
                    continue;
                }else{
                    System.out.println(num);
                }
            }
            // 3 1
    
            num = 5;
            while(num>0){
                if(num == 3){
                    break;
                }
                System.out.println(num);
                num--;
            }
            // 5 4
        }
    }

    do while

    while๊ณผ ๋น„์Šทํ•˜๊ฒŒ ์กฐ๊ฑด์ด ์ฐธ์ผ ๊ฒฝ์šฐ ๋กœ์ง์ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. while๊ณผ์˜ ์ฐจ์ด๋Š” ์กฐ๊ฑด์„ ํ™•์ธํ•˜๊ธฐ ์ „์— ๋จผ์ € ๋กœ์ง์„ ํ•œ ๋ฒˆ ์‹คํ–‰ํ•œ ํ›„ ์กฐ๊ฑด์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

    public class Main {
        public static void main(String[] args) {
    
            int num = 0;
    
            // ์‹คํ–‰ X
            while(num!=0){
                System.out.println("while๋ฌธ ์‹คํ–‰");
            }
    
            // ์‹คํ–‰ O
            do{
                System.out.println("do while๋ฌธ ์‹คํ–‰");
            }while (num!=0);
        }
    }

    while๋ฌธ ๊ฐ™์€ ๊ฒฝ์šฐ ์กฐ๊ฑด์„ ๋จผ์ € ํ™•์ธํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‹คํ–‰์ด ๋˜์ง€ ์•Š์ง€๋งŒ do while๋ฌธ์€ ๋กœ์ง์„ ํ•œ ๋ฒˆ ์‹คํ–‰ ํ›„ ์กฐ๊ฑด์„ ํ™•์ธํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

    ๋ฐ˜์‘ํ˜•
Designed by Tistory.