조건식:
식은 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 때는 같은 키워드 대신 else if키워드 elif 를 사용할 수 있습니다.
예시
다음 예제에서는 식을 사용하는 방법을 보여 줍니다 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!
참고하십시오
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET