WHILE
while
while (์กฐ๊ฑด์) {
์กฐ๊ฑด์์ ๊ฒฐ๊ณผ๊ฐ ์ฐธ์ธ ๋์ ๋ฐ๋ณต์ ์ผ๋ก ์คํํ๊ณ ์ ํ๋ ๋ฌธ์ฅ;
}
์คํ ์์
์กฐ๊ฑด์
์กฐ๊ฑด์ ์ด ์ฐธ์ผ ๊ฒฝ์ฐ ๋ฌธ์ฅ ์ํ
์กฐ๊ฑด์์ด ๊ฑฐ์ง์ด ๋ ๋ ๊น์ง ๋ฐ๋ณต
์๋ ์์ ์ฝ๋๋ก ํ์ตํด ๋ณด๊ฒ ์ต๋๋ค.
class Control5_1 {
public static void main(String[] args) {
int i = 10; // while ๋ฐ๋ณต ํ์ , ์ฆ for ๋ฌธ์ ์ด๊ธฐํ
// while (i-- != 0) {
// System.out.println(i);
// }
// ์ ์ฝ๋์ ๊ฐ์ ๋์์ ์ํํฉ๋๋ค.
while (i != 0) {
i--;
System.out.println(i);
}
}
}
class Control5_2 {
public static void main(String[] args) {
int sum = 0;
int i = 0;
while (sum <= 100) {
System.out.println("i = " + i);
System.out.println("sum = " + sum);
sum += ++i;
}
}
}
do - while
do {
์กฐ๊ฑด์์ ๊ฒฐ๊ณผ๊ฐ ์ฐธ์ธ ๋์ ๋ฐ๋ณต์ ์ผ๋ก ์คํํ๊ณ ์ ํ๋ ๋ฌธ์ฅ;
} while (์กฐ๊ฑด์);
์คํ ์์
์ฒ์ ํ ๋ฒ์ ๋ฌด์กฐ๊ฑด ์คํ
์กฐ๊ฑด์
์กฐ๊ฑด์ ์ด ์ฐธ์ผ ๊ฒฝ์ฐ ๋ฌธ์ฅ ์ํ
์กฐ๊ฑด์์ด ๊ฑฐ์ง์ด ๋ ๋ ๊น์ง ๋ฐ๋ณต
์๋ ์์ ์ฝ๋๋ก ํ์ตํด ๋ณด๊ฒ ์ต๋๋ค.
class Control5_3 {
public static void main(String[] args) {
int j = 1;
do {
System.out.println("do / while ๋ฌธ์ด " + j + "๋ฒ์งธ ๋ฐ๋ณต ์คํ์ค์
๋๋ค.");
j++; // ์ด ๋ถ๋ถ์ ์ญ์ ํ๋ฉด ๋ฌดํ ๋ฃจํ์ ๋น ์ง๊ฒ ๋จ.
} while (j < 20);
System.out.println("do / while ๋ฌธ์ด ์ข
๋ฃ๋ ํ ๋ณ์ j์ ๊ฐ์ " + j + "์
๋๋ค.");
}
}
Last updated