출처 : 자바의 정석 책을 보며 정리 및 공부하고 있습니다. 


 반복문은 어떤 작업을 반복적으로 수행할 때 사용한다. for문과 while은 구조와 기능이 비슷하나 for문은 주로 반복 횟수를 알고 있을 때 사용한다. 


 for문 : 반복 횟수를 알고 있을 때 적합


1
2
3
for (int i = 1; i <=5; i++) {   // 1부터, 5까지, 1씩 증가
    system.out.println("Hello");
}
cs


예제)

1
2
3
4
5
6
7
8
9
10
11
public class RepeatEx01 {
 
    public static void main(String[] args) {
        int sum = 0// 합계를 저장하기 위한 변수
        
        for (int i=1; i<=10; i++) {
            sum = sum + i;
            System.out.println(" i : " + i + " sum : " + sum);
        }
    }
}
cs


중첩 for문 

예제)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class RepeatEx02 {
 
    public static void main(String[] args) {
        for (int i=1; i<=5; i++) {
            for (int j=1; j<=10; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
 
/*
결과)
**********
**********
**********
**********
**********
*/
cs



예제2)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.Scanner;
 
public class RepeatEx03 {
 
    public static void main(String[] args) {
        int num = 0;
        
        System.out.print(" *을 출력할 라인의 수를 입력하세요 : ");
        
        Scanner scanner = new Scanner(System.in);
        String tmp         = scanner.nextLine();
        num                = Integer.parseInt(tmp);
        
        for (int i=0; i<num; i++) {
            for (int j=0; j<=i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
 
/*
숫자 4입력 시 결과
 *을 출력할 라인의 수를 입력하세요 : 4
*
**
***
****
*/
cs


예제3)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class RepeatEx04 {
 
    public static void main(String[] args) {
        for (int i=1; i<=4; i++) {
            for (int j=1; j<=4; j++) {
                System.out.printf("[%d, %d]", i, j);
            }
            System.out.println();
        }
    }
}
 
/*
결과)
[1, 1][1, 2][1, 3][1, 4]
[2, 1][2, 2][2, 3][2, 4]
[3, 1][3, 2][3, 3][3, 4]
[4, 1][4, 2][4, 3][4, 4]
*/
cs



예제4)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class RepeatEx05 {
 
    public static void main(String[] args) {
        for (int i=1; i<=5; i++) {
            for (int j=1; j<=5; j++) {
                if (i!=j) { // i와 j의 값이 다를 때 출력
                    System.out.printf("[%d, %d]", i, j);
                } else {
                    System.out.printf("%5c",' ');
                }
            }
            System.out.println();
        }
    }
}
 
/*
결과)
     [1, 2][1, 3][1, 4][1, 5]
[2, 1]     [2, 3][2, 4][2, 5]
[3, 1][3, 2]     [3, 4][3, 5]
[4, 1][4, 2][4, 3]     [4, 5]
[5, 1][5, 2][5, 3][5, 4]     
*/
cs



향상된 for문 (enhanced for statement)

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class EnhancedForEx01 {
 
    public static void main(String[] args) {
        int arr[] = {1,2,3,4,5};
        
        for (int tmp : arr) {
            System.out.println(tmp);
        }
    }
}
 
/*
결과)
1
2
3
4
5
*/
cs


+ Recent posts