조건식: if...then...else

식은 if...then...else 다른 코드 분기를 실행하고 지정된 부울 식에 따라 다른 값으로 평가됩니다.

구문

if boolean-expression then expression1 [ else expression2 ]

설명

이전 구문 에서 expression1 은 부울 식이 계산되면 true실행되며, 그렇지 않으면 expression2 가 실행됩니다.

다른 언어와 if...then...else 마찬가지로 구문을 사용하여 코드를 조건부로 실행할 수 있습니다. F# if...then...else 에서 식이며 실행되는 분기로 값을 생성합니다. 각 분기의 식 형식이 일치해야 합니다.

명시적 else 분기가 없으면 전체 형식은 unit이며 분기의 then 형식도 이어야 unit합니다.

식을 함께 연결할 if...then...else 때는 키워드(keyword) elif 대신 else if사용할 수 있습니다.

예시

다음 예제에서는 식을 사용하는 방법을 보여 줍니다 if...then...else .

let test x y =
  if x = y then "equals"
  elif x < y then "is less than"
  else "is greater than"

printfn "%d %s %d." 10 (test 10 20) 20

printfn "What is your name? "
let nameString = System.Console.ReadLine()

printfn "What is your age? "
let ageString = System.Console.ReadLine()
let age = System.Int32.Parse(ageString)

if age < 10 then
    printfn "You are only %d years old and already learning F#? Wow!" age
10 is less than 20
What is your name? John
How old are you? 9
You are only 9 years old and already learning F#? Wow!

참고 항목