F # Tutorial: While statement

Source: Internet
Author: User
Tags reference

There is also a while statement in F #. Next we're going to try to write code using the while statement.

In the article "invariance", some people commented that ref can be applied and we tried to use it.

The scope of the while loop is represented by indentation.

let num = ref 0 
while !num < 10 do
         num := !num + 1 
         printfn "%d" !num

Ref is a keyword that represents a reference. It would be nice to think of it as a pointer to the C language. num is a pointer variable, keeping a reference to the value 0. Use when taking the value! Operator. To use when assigning value: =. This is Pascal and similar.

The code above is the ordinal representation of 1 to 10. But the readability is poor. I think it's possible that the while loop is rarely used in F #.

The code for the same functionality overridden by the For statement is as follows:

for num = 1 to 10 do
         printfn "%d" num

The following code is OK as well:

for num in 1..10 do
         printfn "%d" num

The For statement is a process, which is important when used.

But if you want to write a solution from 1 to 10 code, the for statement is not. This is, of course, in cases where ref or mutable are not used.

About this and listen to let's.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.