반복문 (for, while, do-while)
4.1 for 문
초기화, 조건, 증감식 활용
for
문은 초기화, 조건, 증감식을 이용하여 반복 작업을 수행합니다.
for (int i = 1; i <= 5; i++) {
System.out.println("반복 횟수: " + i);
}
중첩 for 문
for
문을 중첩하여 다양한 패턴의 반복 작업을 수행할 수 있습니다.
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
System.out.println("i: " + i + ", j: " + j);
}
}
4.2 while 문
조건에 따른 반복
while
문은 조건이 참인 동안 반복 작업을 수행합니다.
int count = 0;
while (count < 5) {
System.out.println("카운트: " + count);
count++;
}
무한 루프와 탈출 조건
while
문을 사용하여 무한 루프를 생성하고, 탈출 조건을 설정할 수 있습니다.
int number = 0;
while (true) {
if (number >= 5) {
break; // 탈출 조건
}
System.out.println("숫자: " + number);
number++;
}
4.3 do-while 문
조건 검사 후 반복
do-while
문은 코드 블록을 먼저 실행하고 조건을 검사하여 반복 작업을 수행합니다.
int x = 0;
do {
System.out.println("x: " + x);
x++;
} while (x < 5);
Last updated