The for...to expression is used to iterate in a loop over a range of values of a loop variable.
Syntax
for identifier = start [ to | downto ] finish do
body-expression
Remarks
The type of the identifier is inferred from the type of the start and finish expressions. Types for these expressions must be 32-bit integers.
Although technically an expression, for...to is more like a traditional statement in an imperative programming language. The return type for the body-expression must be unit. The following examples show various uses of the for...to expression.
// 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
Μπορείτε να βρείτε την πηγή για αυτό το περιεχόμενο στο GitHub, όπου μπορείτε επίσης να δημιουργήσετε και να εξετάσετε ζητήματα και αιτήματα έλξης. Για περισσότερες πληροφορίες, ανατρέξτε στον οδηγό συνεργατών.
.NET σχόλια
.NET είναι ένα έργο ανοιχτού κώδικα. Επιλέξτε μια σύνδεση για να παρέχετε σχόλια:
Συμμετάσχετε στη σειρά meetup για να δημιουργήσετε κλιμακούμενες λύσεις AI που βασίζονται σε πραγματικές περιπτώσεις χρήσης με συναδέλφους προγραμματιστές και ειδικούς.