// For Each...Next Example
var School, Employee;
School = new Array("Principal", "Mr. Garrison", "Chef");
for (Employee in School) {
NSB.Print("School employee: " + School[Employee]);
}
Created page with "FOR EACH ''element'' IN ''group'' <br /> :::[''statements''] <br /> ::::[EXIT FOR] <br /> :::[''statements''] <br /> NEXT [''element''] '''Description''' FOR EACH...NEXT rep..." |
No edit summary |
||
(11 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
For Each ''element'' In ''group'' <br /> | |||
:::[''statements''] <br /> | :::[''statements''] <br /> | ||
::::[ | ::::[Exit For] <br /> | ||
:::[''statements''] <br /> | :::[''statements''] <br /> | ||
Next [''element''] | |||
== Description == | |||
For Each...Next repeats a group of statements, once for each element in an array or collection. The required parameter, ''element'', is a variable name that can be used to reference the current element. The required parameter, ''group'', is the name of an array, or collection of objects. The optional component, ''statements'', will be executed as the body of the loop. Any number of optional Exit For statements can be used to exit a loop before it is finished. For Each...Next statements can be nested, and any Exit For statements in a nested loop transfer execution to one level above the loop where the Exit For occurs. | |||
Note: | Note: For Each...Next will not work with arrays of user-defined types. | ||
== Example == | |||
< | <tabber> | ||
JavaScript= | |||
<syntaxhighlight lang="JavaScript"> | |||
School = | // For Each...Next Example | ||
"Chef") | |||
var School, Employee; | |||
School = new Array("Principal", "Mr. Garrison", "Chef"); | |||
for (Employee in School) { | |||
</ | NSB.Print("School employee: " + School[Employee]); | ||
} | |||
</syntaxhighlight> | |||
|-| | |||
BASIC= | |||
<syntaxhighlight lang="vb.net"> | |||
Rem For Each...Next Example | |||
Dim School, Employee | |||
School = Array("Principal", "Mr. Garrison", "Chef") | |||
For Each Employee In School | |||
Print "School employee:", Employee | |||
Next | |||
</syntaxhighlight> | |||
</tabber> | |||
== Output == | |||
<pre> | <pre> | ||
Line 31: | Line 44: | ||
</pre> | </pre> | ||
== Related Items == | |||
[[do...loop|Do...Loop]], [[exit|Exit]], [[for...next|For...Next]], [[while...wend|While...Wend]], [[ForEach]] | |||
[[Category:Language Reference]] | |||
[[ | [[Category:Statements - Flow of control]] |
For Each element In group
Next [element]
For Each...Next repeats a group of statements, once for each element in an array or collection. The required parameter, element, is a variable name that can be used to reference the current element. The required parameter, group, is the name of an array, or collection of objects. The optional component, statements, will be executed as the body of the loop. Any number of optional Exit For statements can be used to exit a loop before it is finished. For Each...Next statements can be nested, and any Exit For statements in a nested loop transfer execution to one level above the loop where the Exit For occurs.
Note: For Each...Next will not work with arrays of user-defined types.
// For Each...Next Example
var School, Employee;
School = new Array("Principal", "Mr. Garrison", "Chef");
for (Employee in School) {
NSB.Print("School employee: " + School[Employee]);
}
Rem For Each...Next Example
Dim School, Employee
School = Array("Principal", "Mr. Garrison", "Chef")
For Each Employee In School
Print "School employee:", Employee
Next
School employee: Principal School employee: Mr. Garrison School employee: Chef