일기 대신 코드 슬쩍
[JAVA] Section5) 반복문 본문
반복문 시작
- while
- do-while
- for
while문1
package loop;
public class While1_1 {
public static void main(String[] args) {
int count = 0;
while (count < 5) {
count += 1;
System.out.println("현재 숫자는: " + count);
}
}
}
while문2
반복 횟수를 정한 뒤에 while문을 작성함
do-while문
- while문과 동일한데 무조건 한 번은 실행
package loop;
public class DoWhile1 {
public static void main(String[] args) {
int i = 10;
do{
System.out.println("i = " + i);
i++;
} while (i<3);
}}
break,continue
- break: 반복문을 즉시 종료하고 나감
- continue: 반복문의 나머지 부분을 건너뛰고 다음 반복으로 진행하는데 사용
for문1
for (1.초기식;2.조건식;4.증감식) {
//3.코드
}
for문2
- for문에서 초기식 조건식 증감식은 선택! 근데 세미콜론으로 구분해줘야 함
- for(;;)이렇게 하면 무한 반복 가능;;
package loop;
public class Break3 {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; ; i++){
sum += i;
if (sum > 10) {
System.out.println(sum);
break;
}
}
}
}
중첩 반복문
- for문 안에 for문~
'JAVA' 카테고리의 다른 글
[JAVA] Section5) Problems2 (0) | 2024.03.09 |
---|---|
[JAVA] Section5) Problems1 (0) | 2024.03.09 |
[JAVA] Section4) Problems2 (0) | 2024.03.07 |
[JAVA] Section4) Problems (0) | 2024.03.07 |
[JAVA] Section4) 조건문 (0) | 2024.03.07 |