수색…
다른 경우라면
다트는 If Else :
if (year >= 2001) {
print('21st century');
} else if (year >= 1901) {
print('20th century');
} else {
print('We Must Go Back!');
}
Dart는 연산자가 3 개인 if 도 있습니다.
var foo = true;
print(foo ? 'Foo' : 'Bar'); // Displays "Foo".
While 루프
Dart에서는 while 루프와 do while 루프가 허용됩니다.
while(peopleAreClapping()) {
playSongs();
}
과:
do {
processRequest();
} while(stillRunning());
루프는 중단을 사용하여 종료 할 수 있습니다.
while (true) {
if (shutDownRequested()) break;
processIncomingRequests();
}
continue를 사용하여 루프에서 반복을 건너 뛸 수 있습니다.
for (var i = 0; i < bigNumber; i++) {
if (i.isEven){
continue;
}
doSomething();
}
For Loop
for 루프에는 두 가지 유형이 허용됩니다.
for (int month = 1; month <= 12; month++) {
print(month);
}
과:
for (var object in flybyObjects) {
print(object);
}
for-in 단순히 반복 할 때 루프는 편리 Iterable 모음. 또한 forEach 메소드는 for-in 처럼 작동하는 Iterable 객체에서 호출 할 수 있습니다.
flybyObjects.forEach((object) => print(object));
또는보다 간결하게 :
flybyObjects.forEach(print);
스위치 케이스
Dart에는 긴 if-else 문 대신 사용할 수있는 스위치 케이스가 있습니다.
var command = 'OPEN';
switch (command) {
case 'CLOSED':
executeClosed();
break;
case 'OPEN':
executeOpen();
break;
case 'APPROVED':
executeApproved();
break;
case 'UNSURE':
// missing break statement means this case will fall through
// to the next statement, in this case the default case
default:
executeUnknown();
}
정수, 문자열 또는 컴파일 타임 상수 만 비교할 수 있습니다. 비교되는 객체는 동일한 클래스의 인스턴스 여야하며 (해당 하위 유형이 아닌) 클래스는 ==를 재정의하지 않아야합니다.
Dart에서 전환의 한 가지 놀라운 점은 비어 있지 않은 case 절이 break로 끝나야하거나 덜 일반적으로 continue, throw 또는 return해야한다는 것입니다. 즉, 비어 있지 않은 case 절이 넘어갈 수 없습니다. 비어 있지 않은 case 절을 명시 적으로 끝내야합니다. 대개 break를 사용합니다. break, continue, throw 또는 return을 생략하면 런타임에 해당 위치에서 코드가 오류가 발생합니다.
var command = 'OPEN';
switch (command) {
case 'OPEN':
executeOpen();
// ERROR: Missing break causes an exception to be thrown!!
case 'CLOSED': // Empty case falls through
case 'LOCKED':
executeClosed();
break;
}
비어 있지 않은 case 폴스 스루를 원하면 continue 및 라벨을 사용할 수 있습니다.
var command = 'OPEN';
switch (command) {
case 'OPEN':
executeOpen();
continue locked;
locked: case 'LOCKED':
executeClosed();
break;
}
Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow