개발모음집

16~18강 반복문 (for문, while문, do-while문) 본문

JAVA

16~18강 반복문 (for문, while문, do-while문)

void 2016. 6. 2. 01:30

 

for(초기화;조건식;증감식){

    반복실행될 문장

}

 


 

ex) forEx


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
33
34
35
36
37
38
39
40
41
42
43
44
45
 
public class forEx {
 
    public static void main(String[] args) {
        
        for(int i=0;i<5;i++){// 지역변수 i
            System.out.println("안녕 디지몬");
        }
        int i=10;//전역변수 i
        System.out.println("i= "+i); // 전역변수 i
        // c에서는 i가 for문을 처리하고 나온 값이 i값이 되는데 java는 for문 처리전 값 그대로이다.
        
        int iii = 5;
        for(;iii>=0;iii--){
            System.out.println("디지몬 친구들");
        }
        System.out.println("iii= "+iii); // 이 경우는 for문의 실행처리를 따른다.
        
        
        /*
        for(;;){// 무한루프
            System.out.println("무한루프");
        }*/
        
        // 1부터 10까지의 합
        int sum=0// 위의 소스가 무한루프이기때문에 위에 주석처리하지않으면 처리할 수 없기 때문에 사용할 수 없는 소스가 되니 위에 소스를 삭제하거나 주석처리해야한다.
        for(int j=1;j<=10;j++){
            sum +=j;
        }
        System.out.println("sum ="+sum);    
        
        
        // 0~100까지의 수에서 짝수의 합을 구해보자
        int sum1=0;
        for(int j=1;j<=100;j++){
            //sum1 +=2*j;
            if(j%2==0){
                System.out.println(sum1+"+"+j);
                sum1 +=j;
            }
        }
        System.out.println("1~100까지 수중 짝수의 합 ="+sum1);
    } // main end
 
// forEx end
cs



 

while(조건식){

    반복실행될 문장

     증감식;

}

 

do{

    반복실행될 문장

}while(조건식)

 

ex) whileEx

 

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
33
34
35
36
37
38
39
40
41
42
 
public class whileEx {
    public static void main(String[] args){
        int i=1;
        while (i<5){
            System.out.println("i= "+i);
            i++;
        }
    
    
        //무한루프 for(;;), while(true)
        // 횟수의 범위가 명확하면 for문, 범위가 달라지면 while문
        /*while(true){
        System.out.println("무한루프");
        }*/
        
        // for문을 while문으로 변경해보자.
        
        for (int j=1; j<=5; j++){
            System.out.println(j);
        }
        int j=1;
        while(j<=5){
            System.out.println(j);
            j++;
        }
        // do ~ while문
        j=1;
        do
            System.out.println("j = "+j);
            j++;
        }while(j<=5);
        // a~z까지 출려하는 do~while문을 작성해보자.
        char a = 'a'//97
        //int z=a+1; //a=a+1; type mismatch가 된다. 그래서 정수형 z에 넣거나 a=(char)(a+1);
        do{
            //System.out.println(a);//'ln'은 줄바꿈
            System.out.print(a);
            a=(char)(a+1);
        }while(a<='z');
    }// main end
}// whileEx end
cs

ex) escapeLiteralEx


1
2
3
4
5
6
7
8
9
10
11
12
public class escapeLiteralEx {
    public static void main(String [] args){
        System.out.print("안녕"+'\n');
        System.out.print("반갑다\n");
        
        System.out.println("국어 \t 영어 \t 수학 \t");
        
        System.out.print("캐리지 리턴 값을 출력해보자!\r 'r이후의 문자열'");
        
        System.out.println("안녕\" 반가워\"");
    }
}
cs




ex) forEx2


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
33
34
35
36
37
38
39
40
 
public class forEx2 {
 
    public static void main(String[] args) {
        for(int i=1;i<4;i++){
            for(int k=0;k<2;k++){
                System.out.println("i= "+i+",k= "+k);                
            }// end of for
            System.out.println("///////////////////////////");
        }// end of for2
        //* 출력해보기
//        *****
//        *****
//        *****
        for(int i=0;i<3;i++){
            for(int j=0;j<5;j++){
                System.out.print("*");
            }
            System.out.println(); // 줄 바꿔주는 역할
        }
        
        
        //구구단(가로출력)
        
        for(int i=2;i<=9;i++){
            for(int j=1;j<=9;j++){
                System.out.print(i+"*"+j+"="+(i*j)+" ");
            }
            System.out.println();
        }
        //구구단 2 (세로출력)
        for(int i=1;i<=9;i++){
            for(int j=2;j<=9;j++){
                System.out.print(j+"*"+i+"="+(j*i)+"\t");
            }
            System.out.println();
        }
    } // end of main()
 
}// end of class
cs


'JAVA' 카테고리의 다른 글

JAVA> error: unreported exception ParseException  (0) 2018.04.18
Java Socket, Chatting Program  (1) 2018.03.22
[JAVA Basic] 14~15강 조건문(=제어문)  (0) 2016.06.02
[JAVA Basic] 10강 ~ 13강 연산자  (0) 2016.06.02
[JAVA Basic] 9강 형변환  (0) 2016.06.01