20.03 ~ 20.08 국비교육/JAVA

7. 제어문 - 반복문(while문, for문)

찹키리 2020. 3. 21. 20:07

<while문>

(1) while (조건 (2)) {
(3) 반복할 실행문; (3)
}

->실행문이 한 줄이라면 중괄호 생략 가능

1) 초기값
2) 범위 조건식
3) 중감치(실행 전 증감할 건지, 실행 후 증감할 건지)

ex.
//1에서 5까지 5번 반복하는 반복문을 만드시오
//1) 초기값: int  = 0
//2) 범위 조건식 i <= 5
//3) 증감치 i++;

 

 

<예제>

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
public class While {
    public static void main(String[] args) {
 
        int j = -5//초기값
        while(j < 0) { //j가 0보다 작으면 실행한다
            System.out.println("번호: " + j);
            j++;
            if(j == -3break;
        }
 
        //반환값: 번호: -5
        //        번호: -4
 
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

 

 

 

<do while문>

(1)
do {
(3)반복할 실행문(3);
} while ( (2) );

 

일단 한 번 실행한 뒤 조건을 비교 -> 적어도 1회 이상 실행된다.

 

 

<예제>

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
public class DoWhile {
    public static void main(String[] args) {
 
        int j = 1//초기값이 조건에 위배되더라도 최소 한 번은 실행됨
        do {
            System.out.println("번호: " + j); //1
            j++//2
            if(j == 5break;
        } while (j < 0);
 
        //반환값: 번호: 1
 
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

 

 

 

<반복문3: For문>

for ( (1) ; (2) ; (3) ) {
반복할 실행문;
}

***주의: 동작 순서에 주의, 후치증가밖에 안 됨
1 -> 2 -> 실행 -> 3

 

 

<예제>

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
public class For {
    public static void main(String[] args) {
 
        for(i = 1; i <= 5; i++) {
            System.out.println("★"); //print()와도 비교
        }
 
        //반환값:★
        //       ★
        //       ★
        //       ★
        //       ★
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

 

 

<Evensum(누적 변수)>

누적 변수는 반복문 시작하기 전에 선언

 

int sum = 0
for ( ) {
sum += i;
}
System.out.println(i);

 

누적변수: sum값에 누적시켜라

 

 

<예제>

 

1
2
3
4
5
6
7
8
9
10
11
12
13
 
public class EvenSum {
    public static void man(String args[]) {
        int sum = 0;
 
        for(i = 1; i <= 100; i++) {
            if((i % 2== 0) {
                sum += i; //sum값에 계속 
            }
        }
    System.out.println("1부터 100까지 짝수의 합: " + sum);
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

 

 

 

<이중 반복문>

for ( 4번 ) {
실행1; -> 4회 실행
for ( 5번 ) {
실행2; -> 20회
}
실행3; -> 4회
}
실행4; -> 1회

 

 

<구구단 예제>

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
public class Gugudan {
    public static void man(String args[]) {
       for(int i = 1; i <= 9; i++) {
            System.out.println(" " + i + "단\t\t" + (i + 1+ "단\t\t" + (i + 1+ "단");
            System.out.println("-----------------------------------")
            for(int j = 1; j <= 9; j++) {
                System.out.print(i + " * " + j + " = " + (i * j) + "\t");
                System.out.print((i    + 1+ " * " + j + " = " + ((i + 1* j) + "\t");
                System.out.print((i + 2+ " * " + j + " = " + ((i + 2* j));
                System.out.println("");
            }
 
        System.out.pritnln("");
        }                
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

 

 

 

<break, break label, continue>

1) break

바로 전 단계로 빠져나간다.

 

 

2) break label

라벨 밖으로 나간다.

 

 

<예제>

 

1
2
3
4
5
6
7
8
9
10
11
12
13
 
public class BreakLabel {
    public static void man(String args[]) {
        outer: for(int i = 0; i <= 2; i++) {
            for(int j = 2; j >= 0; j--) {
                if(i == j) break outer;
                System.out.println("i == " + i + ", j == " + j);
            }//for
            // ---> break
        }//outer
        // ---> outer
    }//main
}//class
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

 

3) continue

해당 실행문만 건너뛰고, 뒤는 마저 실행한다.

 

<예제>

 

 
1
2
3
4
5
6
7
8
9
10
11
12
 
public class Continue {
    public static void main(String args[]) {
 
        for(int i = 0; i <= 2; i++) {
            for(int j = 0; j <= 2; j++) {
                if(i == j) continue;
                System.out.println("i == " + i + ", j == " + j);
            } //in for
        } //out for
    } //main
//class
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

'20.03 ~ 20.08 국비교육 > JAVA' 카테고리의 다른 글

클래스 구조  (0) 2020.03.21
배열  (0) 2020.03.21
6. 제어문 - 조건문(if문, switch문)  (0) 2020.03.21
5. 연산자  (0) 2020.03.21
4. 데이터 타입, 형변환  (0) 2020.03.21