Conditionals (조건문)
원본: Computation Layer 문서(Conditionals)
Computation Layer doesn't have "if," "then," or "else." Instead we use a structure similar to a "switch" in other programming languages. We call it when. It is a list of tests, each followed by what should happen in that situation, and requires a default value (otherwise) if none of the conditions match.
Computation Layer에는 "if", "then", "else"가 없다. 대신 다른 프로그래밍 언어의 "switch"와 비슷한 구조를 쓰고, 이것을 when이라고 부른다. when은 조건 검사를 나열한 목록이고, 각 검사 뒤에 그 경우의 값이 온다. 어느 조건에도 맞지 않을 때 쓸 기본값(otherwise)이 반드시 있어야 한다.
# n here is a local variable. "number" is the name we gave to an expression input on this screen. n = input1.numericValue #we compute a custom feedback string to use in the note. feedback = when isUndefined(n) " " when n < 10 "Way higher." when n > 100 "Way lower." when n < 31 "Higher." when n > 31 "Lower." otherwise "Found it!"
Note that a "when" returns as soon as it finds a matching condition. As a result, when we get to the 4th condition to check whether n is greater than 30, we already know that n is at least 10 and at most 100. This might help you write shorter and cleaner conditions.
when은 맞는 조건을 찾는 즉시 그 값을 돌려준다는 점에 주의한다. 그래서 n이 30보다 큰지 확인하는 네 번째 조건에 이르렀을 때는 n이 10 이상 100 이하라는 것을 이미 알고 있다. 이 성질을 이용하면 조건을 더 짧고 깔끔하게 쓸 수 있다.