F# break from while loop

后端 未结 7 2077
甜味超标
甜味超标 2021-02-18 14:30

There is any way to do it like C/C#?

For example (C# style)

for (int i = 0; i < 100; i++)
{
   if (I == 66)
       break;
} 
7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-18 15:12

    This is really ugly, but in my case it worked.

    let mutable Break = false
    while not Break do
        //doStuff
    
        if breakCondition then
            Break <- true
    done
    

    This is useful for do-while loops, because it guarantees that the loop is executed at least once.

    I hope there's a more elegant solution. I don't like the recursive one, because I'm afraid of stack overflows. :-(

提交回复
热议问题