Java 반복문

노성준·2025년 4월 8일

java 첫 걸음마

목록 보기
4/8

반복문 종류

while 반복문

사용법

while (count < 3) {
    count = count + 1;
    System.out.println("현재 숫자는:" + count);
    }
  1. 조건확인
  2. 코드실행
  3. 반복

do-while

public class DoWhile1 {
   public static void main(String[] args) {
       int i = 10;
       do {
           System.out.println("현재 숫자는:" + i);
           i++;
       } while (i < 3);
   }
}
  1. 코드실행
  2. 조건확인
  3. 반복

for 반복문

 public class For1 {
    public static void main(String[] args) {
        for(int i = 1; i <= 10; i++){
            System.out.println(i);
        }
    }
}

for(초기식; 조건식; 증감식;) {
코드
}
1.초기식(1번만 실행)
2.조건식 확인
3.코드실행
4.증감식
5.반복

break & continue

break: 중간에 반복문을 나와버림 (if문과 함께 사용하면 예외상황을 주기 쉬움)
continue: 반복문을 중간에 다시시작함.

profile
비기너

0개의 댓글