반복문

    반복문 do ~ while

    do ~ while 문 스코프 { }를 최소한 한 번 이상 반복 사용자 입력을 받는 경우 주로 사용 do { // 처음 한 번은 무조건 실행 // 조건식이 만족할 시 반복 수행 } while (조건식); 1 ~ 임의의 수(1~10)까지 의 합계 public void method2() { int nRandom = ((int)(Math.random()*10)+1); int nSum = 0; int i = 0; do { i++; nSum += i; System.out.println("nRandom: " + nRandom); System.out.printf("i: %d, nSum: %d\n", i, nSum); } while(i < nRandom); System.out.println("Total: " + nSu..

    반복문 while

    while 문 조건을 만족시키는 동안 스코프 { }를 반복 수행 반복 횟수를 모를 때 주로 사용 while (조건식) { // 조건에 만족할 시 반복 수행 } exit 입력 시 프로그램 종료 public void method2() { String sInput = ""; Scanner s = new Scanner(System.in); while(sInput.equalsIgnoreCase("exit")) { System.out.printf("Insert String: "); sInput = s.nextLine(); System.out.println(sInput); } System.out.println("Bye~!"); } 출력물 Insert String: asdf asdf Insert String: EXIT..

    중첩 for 문

    중첩 for 문 for문 안에 또 다른 for문 포함 for(초기화; 조건식; 증감식) { for(초기화; 조건식; 증감식;) { // 1, 2번째 for문 조건에 만족할 시 반복 수행 } // 1번째 for문 조건에 만족할 시 반복 수행 } 구구단 세로로 출력 public void method2() { for(int nDan = 2; nDan

    반복문 for

    for 문 반복하는 횟수를 알고 있을 때 주로 사용 for(1️⃣초기화; 2️⃣조건식; 4️⃣증감식) { // 3️⃣조건에 만족할 시 반복 수행 } 예제 public void method1() { for(int i = 0; i 0; i--)System.out.println(i); } 출력물 0 1 2 3 2 1 1 ~ 20 까지 짝수 출력 public void method1() { for(int i = 2; i

    반복문(제어문)

    반복문 프로그램 수행 흐름을 바꾸는 역할을 하는 제어문 조건을 만족하는 동안 스코프 { }를 반복 수행 종류 for 문 중첩 for문 while 문 do ~ while 문