1)while
while(조건){
반복 실행 영역
}
package org.opentutorials.javatutorials.loop;
public class WhileDemo {
public static void main(String[] args) {
while (true) {
System.out.println("Coding Everybody"); //무한 반복
}
}
}
while(false){
System.out.println("Coding Everybody"); //실행조차 안됨.
}
int i = 0;
// i의 값이 10보다 작다면 true, 크다면 false가 된다. 현재 i의 값은 0이기 때문에 이 반복문은 실행된다.
while(i<10){
System.out.println("Coding Everybody"+i);
// i의 값에 1을 더한다. 반복문의 중괄호의 마지막 라인에 도달하면 반복문은 반복문을 재호출한다. 이때 i<10의 값을 검사하게 된다.
i++;
}
2)for
for(초기화; 종료조건; 반복실행){
반복적으로 실행될 구문
}
package org.opentutorials.javatutorials.loop;
public class ForDemo {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println("Coding Everybody " + i);
}
}
}
3)break, continue
반복작업을 중간에 중단시키고 싶다면 어떻게 해야 할까? break를 사용하면 된다.
그럼 실행을 즉시 중단하면서 반복은 지속해가게 하려면 어떻게 해야 할까? continue를 사용하면 된다.
'자바' 카테고리의 다른 글
| 13) 메소드 (0) | 2021.10.18 |
|---|---|
| 12) 배열 (0) | 2021.10.18 |
| 10) 논리 연산자 (0) | 2021.10.18 |
| 9) switch (0) | 2021.10.18 |
| 8) 비교와 Boolean (0) | 2021.10.18 |