루프: for...to 식(F#)

for...to 식은 루프 변수의 값 범위에 대해 루프를 반복하는 데 사용됩니다.

for identifier = start [ to | downto ] finish do
   body-expression

설명

식별자의 형식은 start 및 finish 식의 형식을 기준으로 유추됩니다. 이들 식의 형식은 32비트 정수여야 합니다.

기술적인 측면에서 볼 때 for...to 식은 일반적인 명령형 프로그래밍 언어의 문에 더 가깝습니다. body-expression의 반환 형식은 unit이어야 합니다. 다음 예제에서는 for...to 식을 사용하는 여러 가지 방법을 보여 줍니다.

// A simple for...to loop.
let function1() =
  for i = 1 to 10 do
    printf "%d " i
  printfn ""

// A for...to loop that counts in reverse.
let function2() =
  for i = 10 downto 1 do
    printf "%d " i
  printfn ""

function1()
function2()

// A for...to loop that uses functions as the start and finish expressions.
let beginning x y = x - 2*y
let ending x y = x + 2*y

let function3 x y =
  for i = (beginning x y) to (ending x y) do
     printf "%d " i
  printfn ""

function3 10 4

위 코드는 다음과 같이 출력됩니다.

1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

참고 항목

참조

루프: for...in 식(F#)

루프: while...do 식(F#)

기타 리소스

F# 언어 참조