Menu
CoddyTech

불리언 조건

Coddy의 Swift 개발자를 위한 Dart 코스 레슨. 48개 중 7번째.

Swift와 Dart의 조건식에는 truthiness가 아니라 Boolean 값이 필요합니다. 두 언어 모두 단락 평가를 하므로 오른쪽 피연산자를 건너뛸 수 있습니다. Optional Bool과 nullable bool도 일반 조건식으로 사용하기 전에 명시적으로 처리해야 합니다.

조건식과 논리 연산자의 피연산자는 bool이어야 합니다. 단락 평가 연산자의 오른쪽은 필요한 경우에만 실행되므로, 출력을 내는 콜백이 아예 건너뛰어질 수도 있습니다.

완전한 Swift 프로그램:

import Foundation
func probe()->Bool{print("probe");return true}
if false && probe(){print("open")}else{print("closed")}

완전한 Dart 프로그램:

bool probe(){print('probe');return true;}
void main(){if(false && probe()){print('open');}else{print('closed');}}
challenge icon

챌린지

쉬움

온도가 10 이상 80 이하이고 문이 닫혀 있을 때만 true를 반환하도록 safe(int temperature, bool doorClosed)를 구현하세요. 입력은 온도와 true 또는 false입니다. 소문자 불리언 텍스트를 반환하세요.

편집 가능한 파일에서 출력하지 마세요. 제공된 exercise.dart는 입력을 분석하고 출력을 반환합니다. 잠긴 드라이버는 공개 API를 별도로 테스트하고 결과를 줄바꿈 하나와 함께 출력합니다.

코드는 functions.dart, exercise.dart에 작성하세요. main.dart에는 제공된 입출력 코드가 있으며 편집할 수 없습니다.

직접 해보기

import 'dart:io';
import 'dart:convert';
import 'exercise.dart';
import 'functions.dart';
void check(bool condition){if(!condition)throw StateError('Public API contract failed');}
Future<void> main() async {
final raw=await stdin.transform(utf8.decoder).join();
final input=raw.replaceFirst(RegExp(r'\r?\n$'),'');
check(safe(40,true));check(!safe(40,false));
stdout.writeln(solve(input));
}
quiz icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

Swift 개발자를 위한 Dart의 모든 레슨

직접 연습해 보세요: 온라인 Dart 컴파일러