1 / 19

제 9 장 제어문장

제 9 장 제어문장. 목 차. 문장과 블록 조건 분기분 : if ~ else 다중 선택 구조 : case 반복문 break 문 continue 문 return 문. 문장과 블록. 자바 프로그램의 기본적인 문장 연산식 (Expression Statements) 수식 다음에 세미콜론 (;) 을 기술 세미콜론 (;) : 문장의 마지막을 의미 형태 할당문 i = 10; 증감 연산 i++; 메소드 호출 dest = valueOf(x);

Download Presentation

제 9 장 제어문장

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. 제 9장 제어문장

  2. 목 차 • 문장과 블록 • 조건 분기분 : if ~ else • 다중 선택 구조 : case • 반복문 • break 문 • continue 문 • return 문 JAVA 프로그래밍

  3. 문장과 블록 • 자바 프로그램의 기본적인 문장 • 연산식 (Expression Statements) • 수식 다음에 세미콜론(;)을 기술 • 세미콜론(;) : 문장의 마지막을 의미 • 형태 • 할당문 i = 10; • 증감 연산 i++; • 메소드 호출 dest = valueOf(x); • 객체 생성 표현 dest = new Object(); • 선언문 (Declaration Statements) • 변수의 선언과 변수의 초기값 할당을 포함한 것 • 지역변수는 사용되기 전에 선언문이나 초기화 문장 또는 할당문에 의해 초기화 되어야 함 JAVA 프로그래밍

  4. 자바의 제어문 • 조건 제어문 • if • switch ~ case • 반복 제어문 • for • While • 이동 제어문 • break • continue • return JAVA 프로그래밍

  5. 조건 분기문 : if - else • 조건문이 참이면 statements_if 부분을 • 조건문이 거짓이면 statements_else 부분을 실행 If (조건식) { statements_if; } Else { Statements_else; } JAVA 프로그래밍

  6. 조건 분기문 : if – else 예제 [프로그램] class ControlStatement { public static void main( String args[ ] ) { int a = 100; if ( a == 100 ) System.out.println("a는 100과 같다."); else System.out.println("a는 100이 아니다."); } } [실행 결과] a는 100과 같다. JAVA 프로그래밍

  7. 다중 선택 구조 : switch ~ case • 정수식을 평가하여 그 값에 부합되는 case 선택 항목으로 프로그램의 제어를 옮겨줌 Switch (정수식) { case 정수값-1 : Statements-1; break; case 정수값-2 : Statements-2; break; default : Statements-n; } JAVA 프로그래밍

  8. 다중 선택 구조 : switch ~ case 예제 class caseExample { public static final int small = 3, same = 4, large = 5; public static void main(String args[]) { int comcase = 4; switch( comcase ) { case small : // comcase == small인 경우 System.out.println(" comcase의 값은 4보다 작습니다."); break; case same : // comcase == same인 경우 System.out.println(" comcase의 값은 4와 같습니다."); break; case large : // comcase == large인 경우 System.out.println(" comcase의 값은 4보다 큽니다."); break; default : // comcase가 아무값과도 같지 않을 경우 System.out.println(" comcase와 비교할 수 없습니다."); break; } } } JAVA 프로그래밍

  9. 반복문 (while 문) • 형식 While (조건) { Statements; } JAVA 프로그래밍

  10. 반복문 (while 문) 예제 class whileExample public static void main( String args[ ] ) { int i = 0, hap = 0; while ( i <= 100 ) { // i값이 100보다 작거나 같을 때까지 while 문을 수행 hap = hap + i; i = i + 1; } System.out.println("hap="+hap); } } hap = 5050 JAVA 프로그래밍

  11. 반복문 (do-while 문) • 주어진 조건이 참인 경우에 반복 • 수행후에 조건 테스트 do { Statements; } while(조건); JAVA 프로그래밍

  12. 반복문 (do-while 문) 예제 class doWhileTest { public static void main(String args[]) { int j = 6; do { System.out.println("Countdown Start!!!"); System.out.println("j = " + j); } while( j < 5); } } Countdown Start!!! j = 6 JAVA 프로그래밍

  13. 반복문 (for문) • 제어 변수가 초기값을 가지고 수행을 시작 제어 변수에 증감 연산후 조건식 테스트 조건식에 참인 동안 statements를 반복 수행 for (초기식; 조건식; 증감식) statements; JAVA 프로그래밍

  14. 반복문 (for문) 예제 class forExample { public static void main( String args[ ] ) { int i, hap = 0; for ( i = 0; i <= 100; i++ ) // i는 1부터 100까지 1씩 증가 hap = hap + i; System.out.println( " hap = " + hap ); } } hap = 5050 JAVA 프로그래밍

  15. Break 문 • 현재 프로그램을 수행하고 있는 블록으로부터 탈출하기 위해 사용 • Switch문이나 for, while, do-while 루프 블록을 종료시키는 역할을 함 JAVA 프로그래밍

  16. Break 문 예제 class BreakTest { public static void main(String args[]) { int i, j = 1; System.out.println("Countdown Start!"); while(j < 7) { System.out.println("j = " + j); for(i=1; i<=3; i++) { if(j/3 == 1) break; System.out.println(i); } j++; } } } Countdown Start! j = 1 1 2 3 j = 2 1 2 3 j = 3 j = 4 j = 5 j = 6 1 2 3 JAVA 프로그래밍

  17. Continue문 • 루프 안에서 다음 문장을 수행하지 않고 제어가 루프의 논리 조건 비교를 하는 부분으로 바로 이동 JAVA 프로그래밍

  18. Return문 • 메소드의 실행을 종료하고 호출한 부분으로 프로그램의 제어를 되돌리는데 사용 • 만약 메소드가 반환값을 갖지 않으면 단지 return 문만 쓰면 됨 • 메소드가 반환되는 특정 타입을 가지면 return문은 반드시 그 형에 할당할 수 있는 형의 표현식을 포함해야 함 return; // return 값이 없을 때 return 100; // return 값이 있을 때 return a; // 변수 a의 값을 return return (a+","+b)// 연산식의 결과를 return할 때 JAVA 프로그래밍

  19. Return문 예제 –n 계승을 구하는 문제 class Factorial2 { public static int fact(int n){ if(n==1) return 1; else return n*fact(n-1); } public static void main(String[] args) { int n=5; System.out.println(n+" factorial is " + fact(n)); } } [실행 결과] 5 factorial is 120 JAVA 프로그래밍

More Related