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๋ฅผ ์ถฉ์กฑํ ๋์ 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