void main() {
//for loop
for(int i = 0; i < 10; i++){
print(i);
}
}
()
안에 3가지 조건을 설정한다.
int i = 0
i < 10
i++
void main() {
//for loop
int total = 0;
List<int> numbers = [1, 2, 3, 4, 5, 6];
for(int i = 0; i < numbers.length; i++){
total += numbers[i];
}
print(total);
리스트에 있는 값들을 하나씩 꺼내 총합을 구하는 for 문이다.
for in 을 사용해서 다른 방식으로 작성할 수 있다.
total = 0;
for(int number in numbers){
total += number;
}
print(total);
}
void main(){
int total = 0;
while(total < 10){
total += 1;
}
print(total);
}
()
안에 들어갈 조건이 실행 가능해야 한다.total < 10
말고 total < -1
로 설정하면 무한 loop에 빠지게 된다.void main(){
int total = 0;
while( total < 10){
total += 1;
if(total == 5 ){
break;
}
}
print(total);
}
=> 5
break
를 사용하면 그 조건을 만족하면 반복문이 끝나게 된다.void main(){
for(int i = 0; i < 10; i++){
if(i == 5){
continue;
}
print(i);
}
}
0
1
2
3
4
6
7
8
9
continue
는 현재 loop만 건너뛰고 실행할 때 사용한다.enum Status{
approved, //승인
pending, //대기
rejected, //거절
}
void main(){
Status status = Status.approved;
if(status == Status.approved){
print('승인');
}else if(status == Status.pending){
print('대기');
}else{
print('거절');
}
}