코드 작성
package do_while_statement; public class Ex1 { public static void main(String[] args) { /* * do ~ while 문 * * < 기본 문법 > * * 초기식; * * do { * // 실행할 문장들.... * 증감식; * * } while (조건식); // 세미콜론 필수! * */ int i = 11; while (i <= 10) { System.out.println(i + " : Hello, World!"); i++; } System.out.println("반복문 종료 후 i값 : " + i); System.out.println("============================"); // do ~ while 문을 사용할 경우 i = 11; // i가 11이므로 while 조건식 결과가 false 이지만 // 일단 do 문에 의해 반복 내용을 한 번 실행한 후 // 마지막에 while 조건식을 판별하여 false 가 되어 빠져나감 do { System.out.println(i + " : Hello, World!"); i++; } while (i <= 10); System.out.println("반복문 종료 후 i값 : " + i); } }
do ~ while문의 사용법과 예시!
출력 결과
반복문 종료 후 i값 : 11
============================
11 : Hello, World!
반복문 종료 후 i값 : 12
나의 소감
일단 하고(do) 본다
'JAVA' 카테고리의 다른 글
메서드 연습 | 21/11/16 (0) | 2022.01.06 |
---|---|
메서드 | 21/11/16 (0) | 2022.01.06 |
for문 활용 | 21/11/15 (0) | 2022.01.06 |
break문 VS continue(feat. label) | 21/11/15 (0) | 2022.01.06 |
break VS continue 문 | 21/11/15 (0) | 2022.01.06 |