1 / 24

3. while 문

3. while 문. 반복문의 종류 while 문. while( 조건식 )  문장 ;. 3. while 문. while 문 예제 1. 3. while 문. while 문 예제 2. 3. while 문. while 문 예제 3 두 수의 최대공약수 (GCD, Common Greatest Divisor) 구하기 유클리드 알고리즘 Worked Example

Download Presentation

3. while 문

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. 3. while문 • 반복문의종류 • while 문 while( 조건식 )  문장;

  2. 3. while문 • while 문 예제 1

  3. 3. while문 • while 문 예제 2

  4. 3. while문 • while 문 예제 3 • 두 수의 최대공약수 (GCD, Common Greatest Divisor) 구하기 • 유클리드 알고리즘 • Worked Example • GCD (1071, 462) 1071 = 2 × 462 + 147= GCD (462, 147) 462 = 3 × 147 + 21 = GCD (147, 21) 462 = 3 × 147 + 21 = GCD (21, 0) 147 = 7 × 21  GCD (1071, 462) = 21 GCD (x, y) ① 두 수 가운데 큰 수를 x, 작은 수를 y라 한다. ② y가 0이면 최대 공약수는 x이다. ③ r ← x % y ④ x ← y ⑤ y ← r ⑥ 단계 ②로 되돌아간다.

  5. 3. while문 • while 문 예제 3

  6. if( 조건 ) { ... ... } while( 조건 ) { ... ... } 조건이 만족되면 여러 번 반복 실행된다. 조건이 만족되면 한번만 실행된다. 3. while문 • if 문과 while 문의 비교

  7. 3. while문 • do-while 문 • do-while 문 예제 do 문장; while(조건)

  8. 3. while문 • if 와 do-while을 동시에 사용한 예제 (숫자맞추기 게임)

  9. 4. for문 • for문: 정해진 횟수만큼 반복하는 구조 for ( 초기화; 조건식; 증감식 ) 문장;

  10. 4. for문 • for문 예제 1

  11. 4. for문 • for문 예제 2

  12. 4. for문 • for문 예제 3 • n! = 1 * 2 * 3 * … * n

  13. 4. for문 • for문 예제 4 • for문 예제 5 (콤마 연산자) ListCharacters.java class ListCharacters { public static void main(String[] args) { for (char c=0; c<128; c++) if (Character.isLowerCase(c)) System.out.println("value: " + (int)c + " character: " + c); } } CommOperator.java class CommOperator { public static void main(String[] args) { for (inti=1, j=i+10; i<5; i++, j=i*2) System.out.println("i = " + i + " j = " + j); } }

  14. 4. for문 • while 루프와 for 루프와의 관계

  15. 4. for문 • 중첩반복문 (nested loop) • 반복문 안에 다른 반복문이 위치 • outer loop과 inner loop을 제어하는 변수가 달라야 한다.

  16. 4. for문 • 중첩반복문 예제

  17. 4. for문 • Java 5에서 추가된 for 구문: foreach 구문 • 연속된 항목들을 순차적으로 훑어 나가기 위해 int 타입의 변수를 따로 사용할 필요없게 해줌 ForEachFloat.java import java.util.*; public class ForEachFloat { public static void main(String[] args) { Random rand = new Random(System.currentTimeMillis()); float[] f = new float[10]; for (inti = 0; i < 10; i++) f[i] = rand.nextFloat(); for (float x : f) System.out.println(x); } } ForEachString.java public class ForEachString { public static void main(String[] args) { for (char c : "안녕하세요".toCharArray()) System.out.print(c + ", "); } }

  18. 4. for문 • 중간 점검 문제 1. 다음 코드의 출력을 쓰시오. for(int i = 1; i < 5; i++) System.out.print(2 * i + " "); 2. 다음 코드의 출력을 쓰시오. for(int i = 10; i > 0; i = i - 2) System.out.println("Student" + n); 3. 다음 코드의 출력을 쓰시오. for(int i = 1; i < 6; i++) for(int j = 5; j >= 1; i--) System.out.println(i + "곱하기“ + j + "은 ” + i*j);

  19. 5. break와 continue • break 문 • 반복 루프의 종료 • break 예제 • 사용자가 입력한 점수들의 평균을 구함 • 음수를 입력하면 종료

  20. 5. break와 continue • break문과 레이블함께 사용하기 import java.util.*; public class Hello { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a; outer_loop; // 1 while (true) { while (true) { a = sc.nextInt(); if (a < 0) break outer_loop; } } // 2 } } break outer_loop; 수행 이후제어 흐름의 이동은 1 위치가 아니라 2 위치이다.

  21. 5. break와 continue • continue 문 • 현재의 반복을 종료하고 다음 반복을 다시 시작 • continue 문 예제 • 문자열을 대상으로‘n’이 존재하는 횟수를 카운팅

  22. 5. break와 continue • break, continue 문과 레이블함께 사용하기 • (1)번의 break; • 안쪽 while문을 벗어나서바깥쪽 while문을 계속 수행 • (2)번의 continue; • 안쪽 while문을 계속 수행 • (3)번의 continue label1; • 안쪽 while문을 벗어나서바깥쪽 while문을 계속 수행 • (4)번의 break label1; • 바깥쪽 while문을 벗어남즉, 모든 반복문이 끝남 • (1)번과 (3)번의 차이는? • (1)번에서는 (5)번 출력문 수행 • (3)번에서는 (5)번 출력문이 무시됨 label1: while (…) { while (…) { // … break; // (1) // … continue; // (2) // … continue label1; // (3) // … break label1; // (4) } System.out.println("in outer-while"); // (5) } System.out.println(“out outer-while");

  23. 5. break와 continue public class LabeledWhile { public static void main(String[] args) { int i = 0; outer: while(true) { System.out.println("Outer while loop"); while(true) { i++; System.out.println("i = " + i); if(i == 1) { System.out.println("continue"); continue; } if(i == 3) { System.out.println("continue outer"); continue outer; } if(i == 5) { System.out.println("break"); break; } if(i == 7) { System.out.println("break outer"); break outer; } } System.out.println("In outer while loop"); } } } • break, continue문과 레이블함께 사용하기예제(Thinking on Java)

  24. 5. break와 continue • 중간 점검 문제 1. 다음코드의출력을쓰시오. int n = 12; while (n > 0) { n = n - 2; if( n == 6 ) break; System.out.println(n); } 2. 1번문제에서break를continue로변경하면어떻게되는가?

More Related