For Each...Next: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
No edit summary
 
(2 intermediate revisions by one other user not shown)
Line 11: Line 11:
Note: For Each...Next will not work with arrays of user-defined types.
Note: For Each...Next will not work with arrays of user-defined types.


== Example (Basic) ==
== Example ==


<pre>
<tabber>
Rem For Each...Next Example
JavaScript=
Dim School
<syntaxhighlight lang="JavaScript">
School = Array("Principal", "Mr. Garrison", "Chef")
For Each Employee In School
  Print "School employee:", Employee
Next
</pre>
 
== Example (JavaScript) ==
<pre>
// For Each...Next Example
// For Each...Next Example


var School;
var School, Employee;
School = new Array("Principal", "Mr. Garrison", "Chef");
School = new Array("Principal", "Mr. Garrison", "Chef");
for (Employee in School) {
for (Employee in School) {
   NSB.Print("School employee: " + School[Employee]);
   NSB.Print("School employee: " + School[Employee]);
}
}
</pre>
</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 ==
== Output ==

Latest revision as of 15:09, 24 July 2019

For Each element In group

[statements]
[Exit For]
[statements]

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: For Each...Next will not work with arrays of user-defined types.

Example

// 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

Output

School employee:     Principal
School employee:     Mr. Garrison
School employee:     Chef

Related Items

Do...Loop, Exit, For...Next, While...Wend, ForEach