반복할 횟수가 정해져 있을 때 사용
for (int num = 0; num < 4; num++) {
System.out.println(num);
}
for문은 초기화, 조건식, 반복 후 실행될 명령어 생략 가능
int num = 0;
for (; num < 4;) { // 세미콜론을 뺴면 안 된다.
System.out.println(num);
num++;
}
반복할 횟수가 정해져 있지 않을 때 사용
int num = 0;
while (num < 4) {
num++;
System.out.println(num);
}
int num = 0;
do {
num++;
System.out.println(num);
} while (num < 4);
반복문(for, foreach, do, dowhile) 내에서 쓰는 키워드
int i = 0;
while (true) {
i++;
if(i > 10) { //반복문 탈출 조건
break; //탈출
}
}
int i = 0;
while (true) { //올라옴
i++;
if(i % 2 == 0) { //반복문 조건식으로 올라가는 조건 (건너뛰고 싶은 것)
continue;
}
int count = 0;
while (count < 10) {
count++;
if (3 < count && count < 7) {
continue;
}
System.out.println(count); //1 2 3 7 8 9 10
};