[Dart] 5장 반복문

1. for 반복문

Dart에서 for 반복문은 주어진 조건이 참인 동안 특정 코드를 반복해서 실행해주는 반복문으로, 주로 인덱스 기반 반복에 사용되는 반복문 입니다. 반복할 횟수가 명확하게 정해져 있을 때 사용됩니다.

void main() {
  for (int i = 0; i < 10; i++) {
    print(i);
  }

  for (int i = 0; i < 5; i++) {
    print('Iteration: $i');
  }
}

2. while 반복문

while 반복문은 조건이 참인 동안 반복하며, 조건이 처음부터 거짓이면 한 번도 실행되지 않습니다.

void main() {
  int i = 0;
  while (i < 5) {
    print("Iteration: $i");
    i++;
  }

  int count = 0;
  while (count < 5) {
    print(count);
    count++;
  }
}

3. do-while 반복문

do-while 반복문은 조건을 검사하기 전 블록을 한 번 실행하며, 최소 한번은 실행되는 반복문입니다. 사용자에게 입력을 받고, 조건에 따라 반복할 때 유용합니다.

void main() {
  int i = 0;
  do {
    print("iteration: $i");
    i++;
  } while (i < 5);
}

요약

  • for 반복문: 명확한 반복 횟수나 인덱스 기반 접근 시 
  • while 반복문: 조건에 따라 반복해야 할 때
  • do-while 반복문: 최소 한 번 실행하고 조건을 체크해야 할 때.

 

 

GitHub - Koras02/dart-bloging

Contribute to Koras02/dart-bloging development by creating an account on GitHub.

github.com

 

LIST

'Mobile > Dart' 카테고리의 다른 글

[Dart] 7장 매개변수와 반환값  (0) 2025.03.25
[Dart] 6장 함수 정의 및 호출  (0) 2025.03.18
[Dart] 4장 조건문  (0) 2025.03.07
[Dart] 3장 연산자  (0) 2025.03.01
[Dart] 2장 변수 및 데이터 타입  (0) 2025.02.28