변수의 초기화
while (탈출 조건식) {
탈출 조건식이 참일 경우 실행되는 코드;
변수의 증감식;
}
public class WhileExam1 {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
}
}
1
2
3
4
5
public class WhileExam2 {
public static void main(String[] args) {
int i = 1;
while(true) {
if (i == 11) break;
System.out.println(i);
i++;
}
}
}
1
2
3
4
5
6
7
8
9
10
public class WhileExam3 {
public static void main(String[] args) {
int i = 0;
while(i++ < 10) {
System.out.println(i);
}
}
}
*위 예제 코드는 좋은 예시는 아님
1
2
3
4
5
6
7
8
9
10
public class WhileExam4 {
public static void main(String[] args) {
int i = 1;
while(i++ < 10) {
if (i % 2 != 0)
continue;
System.out.println(i);
}
}
}
*위 코드는 예제일뿐 while문 안에서 전위 연산자나 후위 연산자는 되도록 사용하지 않을 것
2
4
6
8