1 / 40

자바 기본 다루기 동일한 작업을 반복 처리하는 반복문

자바 기본 다루기 동일한 작업을 반복 처리하는 반복문. 6 장 동일한 작업을 반복 처리하는 반복문. 지정된 반복 횟수만큼 수행하는 for 문 조건을 먼저 검사하는 while 문 조건을 나중에 검사하는 do~while 문. 1. 지정된 반복 횟수만큼 수행하는 for 문. 1. 지정된 반복 횟수만큼 수행하는 for 문. [ 동작 원리 ] ① 처음에 초기값 실행 ② 조건식을 평가하여 참이면 문장 1~ 문장 n 까지 실행 ③ 증감식 실행

lore
Download Presentation

자바 기본 다루기 동일한 작업을 반복 처리하는 반복문

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 자바 기본 다루기동일한 작업을 반복 처리하는반복문

  2. 6장 동일한 작업을 반복 처리하는 반복문 • 지정된 반복 횟수만큼 수행하는 for 문 • 조건을 먼저 검사하는 while문 • 조건을 나중에 검사하는 do~while문

  3. 1. 지정된 반복 횟수만큼 수행하는 for 문

  4. 1. 지정된 반복 횟수만큼 수행하는 for문 [동작 원리] ① 처음에 초기값 실행 ② 조건식을 평가하여 참이면 문장1~문장n까지 실행 ③ 증감식 실행 ④ 조건식 평가하여 참이면 다시 문장1~문장n까지 실행⑤ 거짓이면 반복문에서 벗어나 다음 문장이 실행

  5. 1. 지정된 반복 횟수만큼 수행하는 for 문

  6. 1. 지정된 반복 횟수만큼 수행하는 for 문

  7. <예제>제어변수 값 출력하기 01:class For02{ 02:  public static void main(String[] args) { 03:    inti; 04:   05:    for(i=1; i<=4; i++) 06:      System.out.println(i );   //제어변수 i값을 출력 07:    08:    System.out.println("--->> " + i ); 09:  } 10}

  8. <예제> for문의 다양한 활용 01:class For03{ 02: public static void main(String[] args) { 03:inti; 04: 05:// i의 초기값을 1로 하여 1씩 증가하면서 10일 때까지 반복 06: for(i=1; i<=10; i++) 07:System.out.print(" " + i ); 08:System.out.println("\n ------------------------- >> " ); 09: 10:// i의 초기값을 1로 하여 2씩 증가하면서 10일 때까지 반복 11: for(i=1; i<=10; i+=2) 12:System.out.print(" " + i ); 13:System.out.println("\n ------------------------- >> " );

  9. <예제> for문의 다양한 활용 14: 15:// i의 초기값을 2로 하여 2씩 증가하면서 10일 때까지 반복 16: for(i=2; i<=10; i+=2) 17:System.out.print(" " + i ); 18:System.out.println("\n ------------------------- >> " ); 19: 20:// i의 초기값을 10으로 하여 1씩 감소하면서 1보다 크거나 같을 때까지 반복 21: for(i=10; i>=1; i--) 22:System.out.print(" " + i ); 23:System.out.println("\n ------------------------- >> " ); 24: } 25:}

  10. <예제> 2단 출력하기 01:class For04 { 02: public static void main(String[] args) { 03:inti; //제어변수 선언 04:int a=2; //출력할 단을 저장하는 변수 선언, 2단 출력 05: 06:System.out.println("<<-----" + a + "단----->> " ); 07: for(i=1; i<=9; i++) 08:System.out.println(a + " * " + i + " = " + (a * i) ); 09: } 10:}

  11. <예제> 입력받은 단 출력하기 01:class For04_1 { 02: public static void main(String[] args) { 03:inti, n=0; //출력하고자하는 단을 명령행에서5를 입력 받았다면 04:int a=Integer.parseInt(args[0]); 05:System.out.println("<<-----" + a + "단----->> " ); 06: for(i=1; i<= 9; i++){ 07: n=a*i; 08:System.out.println(a + " * " + i + " = " + n ); 09: } 10: } 11:}

  12. <예제> 입력받은 문자를 입력받은 숫자만큼 반복해서 출력하기 01:class For05{ 02: public static void main(String[] args) { 03:inti; //제어변수 04:int repeat; //반복 횟수를 결정할 변수 05: String outStr=args[0]; 06: repeat = Integer.parseInt(args[1]); 07: for(i=1; i<=repeat; i++) 08:System.out.print(" " + outStr); 09: } 10:}

  13. for문을 이용한 합 구하기

  14. <예제> 1부터 5까지 합 구하기 01:class For06{ 02: public static void main(String[] args) { 03:inti;//제어변수 선언 04:int total = 0;//합을 누적할 변수 total을 선언하고 0으로 초기화 05: 06: for(i=1; i<=5; i++) //제어변수 i이 1부터 5까지 1씩 증가하도록 함 07: total +=i; // total = total + i; 08:System.out.println("1 ~ " + (i-1) + " = " + total); 09: 10: } 11:}

  15. <예제> 팩토리알 구하기 01:class For07{ 02: public static void main(String[] args) { 03:inti; //제어변수 선언 04:int n; 05:intfac = 1;//팩토리알을 누적할 변수 선언 시 초기값을 1로 줌 06: n = Integer.parseInt(args[0]); //키보드에서 5를 입력받았다면 07: 08: for(i=1; i<=n; i++)//1부터 5사이의 자연수를 구함 09fac*=i; //fac=fac*i;//자연수를 곱해서 누적하여 팩토리알을 구함 10:System.out.println( n + "! ----- >> " + fac ); 11: } 12: }

  16. 2. 조건을 먼저 검사하는 while문

  17. 2. for문과 while문 비교

  18. <예제> 1부터 5사이의 자연수 출력하기 01:class While01 02:{ 03: public static void main(String[] args) 04: { 05:inti; 06: for(i=1; i<=5; i++) 07:System.out.print(" " +i); 08:System.out.println("\n-------------------"); 09: 10:i=1; //초기식 11: while(i<=5){ //조건식 12:System.out.print(" " +i); //반복 처리할 문장 13:i++; //증감식 14: } 15: } 16:}

  19. <예제> 다양한 while문 살펴보기 01:class While02 02:{ 03: public static void main(String[] args) 04: { 05:inti=1; 06: while(i++<=4) 07:System.out.print( i + ", " ); 08:System.out.println("\n---------- >>" ); 09: 10:i=1; 11: while(++i<=4) 12:System.out.print( i + ", " ); 13:System.out.println("\n---------- >>" );

  20. <예제> 다양한 while문 살펴보기 14: 15:i=0; 16: while(i++<=4) 17:System.out.print( i + ", " ); 18:System.out.println("\n---------- >>" ); 19: } 20:}

  21. <예제> 1부터 10사이의 짝수의 합 구하기 01:class While04 { 02: public static void main(String[] args) { 03:int n;//제어변수 선언 04:int tot=0;//합을 누적할 변수 선언 05: n=0;//제어변수 n를 0으로 초기화 06: while(n<=8){//제어변수 n이 8보다 작거나 같을 때까지 반복 수행 07: n+=2;//제어변수 n을 2씩 증가한 후 08: tot += n;//제어변수 n을 합을 누적할 변수에 더한다. 09: } 10:System.out.println("tot = "+tot);//반복문에서 벗어나면 합을 출력한다. 11: } 12:}

  22. 3. 조건을 나중에 검사하는 do~while문

  23. <예제> 다양한 반복문과 NULL문에 주의 01:class While03{ 02: public static void main(String[] args) { 03:  inti=0; 04:  while(i++<=4) 05:   System.out.print( i  + ", " ); 06:  System.out.println("\n--------------------- >>" ); 07: 08:  i=1; 09:  do 10:   System.out.print( i + ", " ); 11:  while(i++<=4);  //do while 문은 반드시 세미콜론으로 끝난다. 12:  System.out.println("\n--------------------- >>" );

  24. <예제> 다양한 반복문과 NULL문에 주의 13: 14:    i=0; 15:    while(i++<=4) ;  //NULL 문으로 인식함 16:      System.out.print( i  + ", " ); 17:    System.out.println("\n--------------------- >>" ); 18: 19:    for(i=1; i<=4; i++) ;  //NULL 문으로 인식함 20:      System.out.print( i  + ", " ); 21:    System.out.println("\n-------------------------"); 22: 23:  } 24:}

  25. <문제> 1. 문자와 숫자를 입력받아 문자를 입력받은 숫자만큼 반복 출력하시오.(ex02_D01.java) 2. 1부터 입력받은 값까지의 홀수 출력하는 프로그램을 작성하시오.(ex02_D02.java) 3. 10부터 1사이의 짝수를 10, 8, 6, 4, 2로 출력하는 프로그램을 작성하시오. (ex02_D03.java) 4. A부터 Z사이의 알파벳 출력하는 프로그램을 작성하시오.(ex02_D04.java)

  26. <문제> 5. 1부터 n(입력받기)까지의 7의 배수와 그 합을 구하여 출력하는 프로그램을 작성하시오.(ex02_D05.java) 6. 1 + 1/2+ 1/3+ . . . 1/10의 합을 구하는 프로그램을 작성하시오.(ex02_D06.java) [결과] sum=2.92XXXX 7. 100+97+94+...+1의 합을 구하는 프로그램을 작성하시오.(ex02_D07.java) [결과] sum=1717 8. 1부터 19사이의 정수 값 두 개 입력받아 n의 m승 구하시오.(ex02_D08.java) [결과] 2 ^ 3 = 8

  27. <문제> 9. 다음 프로그램의 실행 결과를 예측해보세요. public class ex02_E07 { public static void main(String[] args){ int old=1; int cur=1; int next; while(cur<100){ System.out.print(cur+" "); next = cur + old; old = cur; cur=next; } System.out.println( ); } }

  28. <문제> 10. 다음 for문과 동일한 결과를 출력하는 반복문은 어느 것일까요? for( int i = min; i <max; i++) {System.out.println(i);} (1) int i = min;while( i < max ) {}  (2) int i = min;do{System.out.println(i++);} while( i< max ); (3) for (int i=min; i<max; System.out.println(++i)); (4) for (int i=min ; i++<max; System.out.println(i));

  29. <문제> 11. 다음 프로그램의 실행 결과를 예측해보세요. public class ForEx02 { public static void main( String[] argv ) { int i = 0; for( ; i <4; i += 2) { System.out.print(i + " "); } System.out.println(i); } }

  30. <문제> 12. 다음 프로그램의 수행 결과값을 예측해 보시오. public class WhileEx05 { public static void main( String[] argv ) { int x = 1, y =6; while (y--) { x++; } System.out.println("x =" + x + "y =" +y); } }

  31. 7장 제어문의 다양한 활용 • for문에 if문 사용하기 • for 안에 for문을 기술하는 다중 for문

  32. <예제> for문 안에 if문 사용하기 01:class E01_01 { 02: public static void main(String[] args) { 03:int n;//제어변수 선언 04:intodd_tot, even_tot;//홀수의 합과 짝수의 합을 누적할 변수 05: //제어변수 n은 1부터 10사이의 자연수 07: for(odd_tot=0, even_tot=0 , n=1; n<=10; n++) 08: if(n%2==1)//n을 2로 나누어서 나머지가 1이면 홀수이므로 09:odd_tot += n;//홀수의 합을 누적하는 변수에 더하고 10: else//n을 2로 나누어서 나머지가 1이 아니고 0이면 짝수이므로 11:even_tot += n;//짝수의 합을 누적하는 변수에 더한다. 12:System.out.println("odd_tot(1+3+5+7+9) = " + odd_tot);//홀수의 합을 출력 13:System.out.println("even_tot(2+4+6+8+10) = " + even_tot); //짝수의 합을 출력 14: } 15:}

  33. 2. for 안에 for문을 기술하는 다중 for문

  34. <예제> 다중 for문에서 제어변수의 변화 알아보기 01:class E08{ 02: public static void main(String[] args) { 03:  inti; 04:  int a; 05:  System.out.println("시침  ----------------->> 분침"); 06:  System.out.println("a(바깥쪽 제어변수)-->> i(안쪽 제어변수)"); 07:  for(a=1; a<5; a++) 08:   for(i=1; i<=5; i++) 09:     System.out.println(a + "----------------- >> " + i ); 10: } 11:}

  35. <예제> 2단부터 9단까지 구구단 출력하기 01:public class E03 { 02: public static void main(String[] args) { 03:intdan; //단을 결정하는 변수 선언 04:int n; 05: 07: for(dan=2; dan<=9; dan++){ //2단부터 9단까지를 구함 08:System.out.println("** " + dan + " 단 **"); 09: for(n=1; n<=9; n++){ 10:System.out.println(dan + " * " + n + " = " + dan * n); 11: } //안쪽 for문의 끝 12: }//바깥쪽 for문의 끝 13: }//main 함수의 끝 14:}

  36. <예제> 숫자를 삼각형 형태로 출력하기 01:public class E04 { 02: public static void main(String[] args) { 03:int a;//바깥쪽 for문의 제어변수 선언 04:int b;//안쪽 for문의 제어변수 선언 05: for(a=1; a<=5; a++){//5줄 반복한다. 06: for(b=1; b<=5; b++){//한 줄에 스타(*)를 5번 출력하기 위한 반복문 07:System.out.print("* "); //안쪽 for문에 의해 반복되는 문장 08: } //안쪽 for문의 끝 09:System.out.println();//줄 바꾸기 위한 문장 10: }//바깥쪽 for문의 끝 11: }//main 함수의 끝 12:}

  37. <문제> 1. 22부터 76까지의 짝수의 개수와 그 합을 구하는 프로그램을 작성하시오.(ex02_E01.java) 짝수의 개수 -> 28 그 합-> 1372 2. 1부터 100사이의 합을 구하되 10 단위마다 한 번씩 출력하시오. 1 ~ 10 까지의 합-> 55 1 ~ 20 까지의 합-> 210 1 ~ 30 까지의 합-> 465 1 ~ 40 까지의 합-> 820 1 ~ 50 까지의 합-> 1275 1 ~ 60 까지의 합-> 1830 1 ~ 70 까지의 합-> 2485 1 ~ 80 까지의 합-> 3240 1 ~ 90 까지의 합-> 4095 1 ~ 100 까지의 합-> 5050

  38. <문제> 3. 1부터 100사이의 짝수를 출력하되 한 줄에 10 개씩 출력하시오. 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 4. 각 단이 아래로 출력되지 않고 오른쪽으로 출력되도록 구구단 프로그램을 작성하시오.

  39. <문제> 5. 스타(*)로 역삼각형을 출력하는 프로그램을 작성하시오. 6. 다음과 같이 알파벳 A부터 I까지를 역삼각형으로 출력하시오. ABCDE FGH I

  40. <문제> 7. 다음 프로그램의 실행 결과를 예측해보세요. public class ForEx03 { public static void main( String[] argv ) { for (int i =0; i <3; i++) { switch(i) { case 0: break; case 1: System.out.print("one "); case 2: System.out.print("two "); case 3: System.out.print("three "); } } System.out.println("done"); } }

More Related