for

  • Updated

For statements are loops that take one of two forms: a C# foreach-style iterator over a collection, or a traditional C#-style for loop.

Here are examples of each style:

var x = ['hi', 'bye', 'hello', 'howdy'];
var y = [];

for( var item in x )
{
if( item.Length < 5 )
{
y.Push( item );
}
}

for( var i = 0; i < y.Count; i++ )
{
y[i] = y[i].Length * 1.5;
}

The traditional C#-style for loop syntax allows for optional preamble, condition, and suffix expressions:

var result = 1;
var i = 0;

for( ; i < 10; i++ )
{
result = i * 1.5;
}
i = 1;
for( ; ; i++ )
{
if( i == 10 )
{
break;
}

result = i * 1.5;
}

i = 1;

for( ; ; )
{
if( i == 10 )
{
break;
}
result = i++ * 1.5;
}

Note that, similar to C# foreach semantics, modifying the target collection inside an iterator-style for loop results in an error.

Break and continue statements are supported in both styles of for loop.

Was this article helpful?

0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.