while & do while

while : ์กฐ๊ฑด์ด true์ผ ๋™์•ˆ ๋ฐ˜๋ณต ์ˆ˜ํ–‰

package sec04.chap04;

public class Ex01 {
    public static void main(String[] args) {
        int i = 0;

        //  ๐Ÿ’ก while ๋ฌธ์˜ ๊ด„ํ˜ธ์—๋Š” ์ข…๋ฃŒ์กฐ๊ฑด๋งŒ
        while (i < 10) {
            // ์ข…๋ฃŒ์กฐ๊ฑด ์ถฉ์กฑ์„ ์œ„ํ•œ ๊ฐ’ ๋ณ€ํ™”๋Š” ์™ธ์ ์œผ๋กœ
            System.out.println(i++);
        }

//        //  ๐Ÿ’ก ์˜๋„์ ์ธ ๋ฌดํ•œ ๋ฃจํ”„์— ๋„๋ฆฌ ์“ฐ์ด๋Š” ์ฝ”๋“œ
//        while (true) {
//            System.out.println("์ธ๊ฐ„์˜ ์š•์‹ฌ์€ ๋์ด ์—†๊ณ ");
//            System.out.println("๊ฐ™์€ ์‹ค์ˆ˜๋ฅผ ๋ฐ˜๋ณตํ•œ๋‹ค.");
//        }

        double popInBillion = 7.837;

        //  โญ๏ธ break ๋ฅผ ํ†ตํ•œ ๋ฐ˜๋ณต ํƒˆ์ถœ
        while (true) {
            System.out.println("์„ธ๊ณ„์ธ๊ตฌ: " + (popInBillion -= 0.1));
            if (popInBillion <= 0) break;

            System.out.println("์ธ๊ฐ„์˜ ์š•์‹ฌ์€ ๋์ด ์—†๊ณ ");
            System.out.println("๊ฐ™์€ ์‹ค์ˆ˜๋ฅผ ๋ฐ˜๋ณตํ•œ๋‹ค.");
        }

        System.out.println("์ธ๋ฅ˜ ๋ฉธ์ข…");
    }
}

package sec04.chap04;

public class Ex02 {
    public static void main(String[] args) {
        //  100๋ณด๋‹ค ์ž‘์€ 3์˜ ๋ฐฐ์ˆ˜๋“ค ์ถœ๋ ฅํ•ด๋ณด๊ธฐ

        int i = 1;

        // โš ๏ธ ์˜๋„๋Œ€๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š์Œ. ์ด์œ ๋Š”?
//        while (true) {
//            if (i % 3 != 0) continue;  // ๐Ÿ”ด
//            System.out.println(i);
//
//            if (i++ == 100) break;
//        }


//        while (true) {
//            if (i++ == 100) break;
//            if ((i - 1) % 3 != 0) continue;
//
//            System.out.println(i - 1);
//        }

        //  ๋ณด๋‹ค ๊ฐ€๋…์„ฑ์„ ๋†’์ด๊ณ  ์˜๋„๋ฅผ ์ž˜ ๋“œ๋Ÿฌ๋‚ธ ์ฝ”๋“œ
        while (true) {
            int cur = i++;

            if (cur == 100) break;
            if (cur % 3 != 0) continue;

            System.out.println(cur);

        }
    }
}

do ... while : ์ผ๋‹จ ์ˆ˜ํ–‰ํ•˜๊ณ  ์กฐ๊ฑด์„ ๋ด„

package sec04.chap04;

public class Ex03 {
    public static void main(String[] args) {
        int enemies = 0;

        System.out.println("์ผ๋‹จ ์‚ฌ๊ฒฉ");

        do {
            System.out.println("ํƒ•");
            if (enemies > 0) enemies--;
        } while (enemies > 0);

        System.out.println("์‚ฌ๊ฒฉ์ค‘์ง€ ์•„๊ตฐ์ด๋‹ค");


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

        int x = 1; // 10 ์ด์ƒ์œผ๋กœ ๋ฐ”๊ฟ”์„œ ๋‹ค์‹œ ์‹คํ–‰ํ•ด ๋ณผ ๊ฒƒ
        int y = x;

        while (x < 10) {
            System.out.println("while ๋ฌธ: " + x++);
        }

        do {
            System.out.println("do ... while ๋ฌธ: " + y++);
        } while (y < 10);
    }

}

package sec04.chap04;

public class Ex04 {
    public static void main(String[] args) {
        final int LINE_WIDTH = 5;

        int lineWidth = LINE_WIDTH;

        while (lineWidth > 0) {
            int starsToPrint = lineWidth--;
            while (starsToPrint-- > 0) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

Last updated