๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Lang/โ˜• Java

โ˜• Java * ๋ฐ˜๋ณต๋ฌธ ( for, for-each, while, do-while )

์‚ฌ์‹ค ํ”„๋กœ๊ทธ๋žจ ์–ธ์–ด์˜ ๋ฐ˜๋ณต๋ฌธ๊ณผ ์กฐ๊ฑด๋ฌธ์€ ๋‹ค ๋น„์Šทํ•˜๋‹ค.

if, while, for ๋ณดํ†ต ์ด ์„ธ๊ฐ€์ง€ ์นœ๊ตฌ๋ฅผ ํ•„๋‘๋กœ else if, do while, for each, switch ๋ญ ์ด๋ ‡๊ฒŒ ๋” ๋‚ด์šฉ์ด ์žˆ๋‹ค.

 

์“ฐ๋Š” ๋ฐฉ๋ฒ•๋„ ๋น„์Šทํ•˜์ง€๋งŒ ์–ธ์–ด์— ๋”ฐ๋ผ ์กฐ๊ธˆ์”ฉ ๋ชจ์–‘์ด ๋‹ค๋ฅธ ๋ถ€๋ถ„๋„ ์žˆ์œผ๋‹ˆ

Java language์—์„œ ๋ฐ˜๋ณต๋ฌธ์„ ์–ด๋–ป๊ฒŒ ์“ฐ๋Š”์ง€ ์•Œ์•„๋ณด์ž.

 

๐Ÿ“Œ ๋ฐ˜๋ณต๋ฌธ

์ž๋ฐ”์—์„œ๋Š” for, for each, while, do while์„ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

for
  • ์•ž์„œ ์„ ์–ธํ•œ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์ดˆ๊ธฐ๊ฐ’์€ ์ƒ๋žต๊ฐ€๋Šฅํ•˜๋‹ค. ex) for ( ; ์กฐ๊ฑด๋ฌธ; ์—ฐ์‚ฐ์‹)
  • ์กฐ๊ฑด๋ฌธ์„ ๋งŒ์กฑํ•˜๋Š” ๋™์•ˆ for body๋ฅผ ๋ฐ˜๋ณตํ•˜์—ฌ ์‹คํ–‰ํ•œ๋‹ค.
  • ํ•œ๋ฒˆ ์‹คํ–‰ ํ›„ ์—ฐ์‚ฐ์‹์ด ์žˆ๋Š” ๊ฒฝ์šฐ ํ•ด๋‹น ์‹์„ ์‹คํ–‰ํ•œ ๋’ค ์กฐ๊ฑด๋ฌธ์„ ๋น„๊ตํ•œ๋‹ค.
    ์—ฐ์‚ฐํ•  ๊ฒƒ์ด ์—†๋‹ค๋ฉด ์—ฐ์‚ฐ์‹๋„ ์ƒ๋žต ๊ฐ€๋Šฅํ•˜๊ธด ํ•˜์ง€๋งŒ ์ด๋Ÿฐ ๊ฒฝ์šฐ๋ผ๋ฉด for๋ฌธ์„ ์“ฐ๋Š” ์ด์œ ๊ฐ€ ์—†์ง€ ์•Š์„๊นŒ ์‹ถ๋‹ค.
  • ์ผ์ •ํ•œ ํšŸ์ˆ˜๋งŒํผ ๋ฐ˜๋ณตํ•  ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋œ๋‹ค.
public class Test {

    public static void main(String[] args) {

        // 01.
        System.out.print("01: ");
        for (int i = 0; i < 5; i++) {
            System.out.print(i + " ");
        }

        System.out.println();
        
        // 02
        System.out.print("02: ");
        int i = 0;
        for (; i < 10; i = add(i)) {
            System.out.print(i + " ");
        }
    }

    public static int add(int i) {
        return i += 2;
    }
    
    
}
01: 0 1 2 3 4
02: 0 2 4 6 8

 

 

for-each
  • ๋ฐฐ์—ด์ด๋‚˜ ๊ฐ’์ด ํ•˜๋‚˜์”ฉ ๋„˜์–ด์˜ค๋Š” ๋ณ€์ˆ˜๋“ค์˜ ์ฒ˜๋ฆฌ์— ์œ ์šฉํ•˜๋‹ค.
  • ๋ณ€์ˆ˜์˜ ํฌ๊ธฐ๋งŒํผ ๋ฐ˜๋ณตํ•˜๋ฉฐ, ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜์”ฉ ๋ฐ›์•„์˜จ๋‹ค.
public class Test {

    public static void main(String[] args) {

        String[] hi = { "์•ˆ", "๋…•", "ํ•˜", "์„ธ", "์š”" };

        for (String ch : hi) {
            System.out.print(ch);
        }
    }

}
์•ˆ๋…•ํ•˜์„ธ์š”

 

 

while
  • ์กฐ๊ฑด๋ฌธ์ด ์ฐธ์ด๋ผ๋ฉด while body๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
  • ๋ณดํ†ต loop(๋ฌดํ•œ ๋ฐ˜๋ณต)์„ ์‹œํ‚ฌ ๋•Œ while์„ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ํŽธ์œผ๋กœ if์™€ break๋ฅผ ๊ฐ™์ด ๋Œ€๋™ํ•œ๋‹ค.
    C๋‚˜ Python์—์„œ๋Š” while(1)์ธ ๊ฒฝ์šฐ๋„ while(true)์™€ ๊ฐ™์ด ๋™์ž‘ํ•˜์ง€๋งŒ java๋Š” ์ •์ˆ˜์™€ boolean ๊ฐœ๋…์ด ๋ถ„๋ฆฌ(?)๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— 1 = true ๊ฐ€ ์„ฑ๋ฆฝ๋˜์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ while(1)์€ ์„ฑ๋ฆฝ๋˜์ง€ ์•Š๋Š”๋‹ค.
  • ๋ฏธ๋ฆฌ ๋ฐ˜๋ณต ํšŸ์ˆ˜๋ฅผ ์•Œ ์ˆ˜ ์—†๊ณ  ์กฐ๊ฑด์— ๋”ฐ๋ผ์„œ ๋ฐ˜๋ณตํ•˜๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉ๋œ๋‹ค.
import java.util.Scanner;

public class Test {

    public static void main(String[] args) {

        int a = 0;
        while (a < 2) {
            System.out.print(a + " ");
            a++;
        }
        

        System.out.println();
        System.out.println("์ˆซ์ž 0์„ ์ž…๋ ฅํ•˜๋ฉด ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.");

        Scanner sc = new Scanner(System.in);
        while (true) {
            if (sc.nextLine().equals("0")) {
                break;
            }
        }

        sc.close();
    }

}
0 1
์ˆซ์ž 0์„ ์ž…๋ ฅํ•˜๋ฉด ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.

 

 

do-while
  • do body๋ฅผ ์‹คํ–‰ ํ›„ ์กฐ๊ฑด์„ ๊ฒ€์‚ฌํ•œ๋‹ค.
    = ์กฐ๊ฑด๋ฌธ์ด ์ฐธ์ด๋“  ๊ฑฐ์ง“์ด๋“  do body๋ฅผ ์ ์–ด๋„ ํ•œ๋ฒˆ์€ ์‹คํ–‰ํ•œ๋‹ค.
  • ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์ „์— ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ๋ฉ”๋‰ด๋‚˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ ๋ฐ›์•„์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์— ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค.
public class Test {

    public static void main(String[] args) {

        int i = 1;
        do {
            System.out.print(i + " ");
            i++;
        } while (i < 3);

    }

}
1 2

 

 

๋!

๋ฐ˜๋ณต๋ฌธ์—์„œ ์ œ์–ด์˜ ํ๋ฆ„์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

๋ฐ”๋กœ break์™€ continue์ธ๋ฐ ์ด๋“ค์€ ๋‹ค๋ฅธ ํฌ์ŠคํŒ…์—์„œ ์‚ดํŽด๋ณด๋„๋ก ํ•˜์ž!