for & forEach

์ฃผ์–ด์ง„ ์กฐ๊ฑด์ด ์ถฉ์กฑ๋˜๋Š” ๋™์•ˆ ํŠน์ • ์ž‘์—…์„ ๋ฐ˜๋ณต

package sec04.chap03;

public class Ex01 {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            System.out.println(i); // ๐Ÿ”ด
        }
    }
}
  • ์‹คํ–‰ ๊ณผ์ •

    1. ๋ฃจํ”„ ์•ˆ์—์„œ ์‚ฌ์šฉํ•  ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™”

    2. ๋ฃจํ”„๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ์กฐ๊ฑด ํ™•์ธ

    3. ์กฐ๊ฑด์„ ์ถฉ์กฑ์‹œ ์‹คํ–‰ํ•  ๋‚ด์šฉ

    4. ๊ฐ ๋ฃจํ”„๊ฐ€ ๋๋‚ ๋•Œ๋งˆ๋‹ค ์ดํ–‰ํ•  ๋‚ด์šฉ

  • 1๋ฒˆ ์ดํ›„ 2๋ฅผ ์ถฉ์กฑํ•  ๋™์•ˆ 2~4 ๋ฐ˜๋ณต

  • ๋ณ€์ˆ˜๋ช…์€ ์›ํ•˜๋Š”๋Œ€๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

    • ์ผ๋ฐ˜์ ์œผ๋กœ ๊ธฐ๋ณธํ˜•์—๋Š” i ๋ฅผ ๋งŽ์ด ์‚ฌ์šฉ - ๋ฌธ๋งฅ์— ๋”ฐ๋ผ index๋ฅผ ๋œปํ•จ

package sec04.chap03;

public class Ex02 {
    public static void main(String[] args) {
        //  ๋ฃจํ”„ ๋ธ”๋ก ์•ˆ์—์„œ ๋ณ€์ˆ˜๊ฐ’์„ ๋ฐ”๊พธ๋Š” ๊ฒƒ์œผ๋กœ 4๋ฒˆ ๊ณผ์ • ๋Œ€์‹  ๊ฐ€๋Šฅ
        for (int i = 0; i < 10;) {
            System.out.println(i);
            i += 2;
        }


        System.out.println("\n- - - - -\n");

        for (double d = 123.45; d > 0; d -= 32.1) {
            System.out.println(d);
        }

        for (String s = ""; s.length() < 11; s += s.length()) {
            System.out.println(s);
        }

        System.out.println("\n- - - - -\n");

        //  ๐Ÿ’ก ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์—ฌ๋Ÿฟ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
        //  โš ๏ธ ๋ณ€์ˆ˜์˜ ์ž๋ฃŒํ˜•์€ ํ•œ ์ข…๋ฅ˜๋งŒ ๊ฐ€๋Šฅ (ํ˜ผ์šฉ ์•ˆ ๋จ)
        for (byte a = 0, b = 10; a <= b;) {
            System.out.printf("a: %d, b: %d%n", a++, b--);
        }
    }
}

๋ฌดํ•œ ๋ฃจํ”„

๋ฌดํ•œ๋ฃจํ”„๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ •์ง€์‹œํ‚ด

package sec04.chap03;

public class Ex03 {
    public static void main(String[] args) {
        //  ์ข…๋ฃŒ์กฐ๊ฑด์ด ์—†๋Š” for ๋ฃจํ”„
        for (;;) {
            System.out.println("์˜์›ํžˆ");
        }
        System.out.println("๋‹ฟ์ง€ ์•Š์•„"); // โš ๏ธ ์‹คํ–‰๋˜์ง€ ์•Š์Œ


        //  ์ข…๋ฃŒ์กฐ๊ฑด์„ ๋งŒ์กฑ์‹œํ‚ค์ง€ ๋ชปํ•˜๋Š” ๋ฌดํ•œ๋ฃจํ”„
        for (int i = 0; i < 10; i++) {
            System.out.println("๊ทธ๋ž˜๋„");
            i--;
        }
        System.out.println("๋‹ฟ์ง€ ์•Š์•„"); // โš ๏ธ ์‹คํ–‰๋˜์ง€ ์•Š์Œ
    }
}

๋ฐฐ์—ด์˜ ๋ฃจํ”„

package sec04.chap03;

public class Ex04 {
    public static void main(String[] args) {
        //  4์˜ ๋ฐฐ์ˆ˜ ์ฐจ๋ก€๋กœ 10๊ฐœ ๋ฐฐ์—ด์— ๋‹ด๊ธฐ
        int count = 10;
        int[] multiOf4 = new int[count];

        for (int i = 1, c = 0; c < count; i++) {
            if (i % 4 == 0) {
                multiOf4[c++] = i;
            }
        }
        //  ๐Ÿ’ก ๋ฐฐ์—ด ์ˆœํ™˜ (๊ธฐ๋ณธ์ ์ธ ๋ฐฉ๋ฒ•)
        for (int i = 0; i < multiOf4.length; i++) {
            System.out.println(multiOf4[i]);
        }
    }
}

์ค‘์ฒฉ ๋ฃจํ”„

package sec04.chap03;

public class Ex05 {
    public static void main(String[] args) {
        //  ๊ตฌ๊ตฌ๋‹จ ์˜ˆ์ œ
        for (int i = 1; i < 10; i++) {
            for (int j = 1; j < 10; j++) {
                System.out.printf("%d X %d = %2d%n", i, j, i * j);
            }
        }

        String[][] quotesInLangs = {
                {
                        "I am vengeance.",
                        "I am night.",
                        "I am Batman.",
                },
                {
                        "๋‚˜๋Š” ๋ณต์ˆ˜๋ฅผ ํ•˜์ง€.",
                        "๋‚˜๋Š” ๋ฐค์ด์ง€.",
                        "๋‚˜๋Š” ๋ฐฐํŠธ๋งจ์ด์ง€.",
                },
        };

        String result = "";
        for (String[] quotes : quotesInLangs) {
            for (String quote : quotes) {
                result += quote + " "; // ๐Ÿ”ด
            }
            result = result.trim().concat("\n");
        }

        System.out.println(result);
    }
}

continue์™€ break

package sec04.chap03;

public class Ex06 {
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {

            //  ๐Ÿ’ก continue : ํ•œ ๋ฃจํ”„๋งŒ ๊ฑด๋„ˆ๋œ€
            if (i % 3 == 0) continue;

            //  ๐Ÿ’ก break : ๋ธ”๋ก ์ „์ฒด๋ฅผ ์ข…๋ฃŒ
            if (i == 10) break;

            System.out.println(i);
        }

        System.out.println("\n- - - - -\n");

        String str = "ํ˜ธ";

        //  โญ๏ธ ๋ฌดํ•œ๋ฃจํ”„ ํƒˆ์ถœ์— ์‚ฌ์šฉ ๊ฐ€๋Šฅ
        for (;;) { // ๋‹ค์Œ ๊ฐ•์˜ while์„ ๋” ๋งŽ์ด ์‚ฌ์šฉ
            str += "๋กค";
            System.out.println(str);
            if (str.length() == 30) break;
        }

        str += "๋กœ";
        System.out.println(str);
    }
}
    System.out.println("\n- - - - -\n");

    //  ๐Ÿ’ก label : ์ค‘์ฒฉ ๋ฃจํ”„์—์„œ ์–ด๋Š์ชฝ์„ continue, break ํ•  ์ง€ ๊ตฌ๋ถ„
    outer:
    for (int i = 0; i < 10; i++) {

        inner:
        for (int j = 0; j < 10; j++) {
            if (j % 2 == 0) continue inner;
            if (i * j >= 30) continue outer;

            if (j > 8) break inner;
            if (i - j > 7) break outer;

            System.out.printf("i: %d, j: %d%n", i, j);
        }
    }

Last updated