GenerativeComponents Help

The Foreach Statement

Provides a convenient way to iterate though all of the items in a list.

Simple form

	foreach (variable in sourceList)
	{
	    body-statements
	}

Where:

  • variable is the name of a variable called the iteration variable, which represents each successive member of the source list. We can pick any name we want; the variable is created spontaneously, and is local to the foreach statement.
  • sourceList is any expression that evaluates to a list. The foreach statement steps through the members of this list without changing it.
  • body-statements is a sequence of one or more statements that are executed repeatedly, once for each member of the sourceList.

The simple form of the foreach statement behaves exactly like the following:

	for (int i = 0; i < sourceList.Count; ++i)
	{
	    var variable = list[i];
	    body-statements
	}

except that there is no index variable 'i'.

General form

The general form of the foreach statement is as follows. The square brackets are not part of the syntax, but indicate those parts that are optional.

	foreach ( variable in sourceList
	             [ where expression ]
	             [ orderby expression(s) ]
	             [ distinct ]
	             [ reverse ]
	             [ skip count]
	             [ take count ] )
	{
	    body-statements
	}

Where:

  • variable, sourceList, and body-statements are as described above.
  • where expression is an optional clause that restricts the list members given to the body-statements to only those for which the specified expression evaluates to true.
  • orderby expression(s) is an optional clause that changes the order in which the list members are given to the body-statements, based on the value(s) of the specified expression(s). Multiple expressions, which represent multiple levels of sorting, are separated by commas. Any expression may be followed by the keyword descending, which reverses the direction of the sort at that level.
  • distinct is an optional clause that prevents duplicate list members from being given to the body-statements.
  • reverse is an optional clause that reverses the order in which the list members are given to the body-statements.
  • skip count is an optional clause that skips a certain number of list members before giving the rest to the body-statements. The count is any expression that resolves to a number.
  • take count is an optional clause that limits the number of list members given to the body-statements. The count is any expression that resolves to a number.

There may be any number of each clause type, and they may appear in any sequence. The clauses are applied in the sequence in which they appear.

Like everything else in GCScript, the foreach header statement may be written on one line or across multiple lines. Multiple lines are often preferred because they are easier to read. However, either of these forms is equally acceptable:

	foreach (ln in line01
	         where ln.Length <= maxLength
	         orderby ln.StartPoint.X)
	{
	    ...
	foreach (ln in line01 where ln.Length <= maxLength orderby ln.StartPoint.X)
	{
	    ...

Specifying the type of the iteration variable

It is possible to explicitly specify the type of the iteration variable, e.g:

	foreach (Line ln in line01)

The primary advantage of doing so is that, subsequently, the script editor with produce a proper auto-completion list for the iteration variable.

For backward compatibility, the generic variable type var is also acceptable in this context, though it doesn't accomplish anything.

Query expressions

The various list selection and sorting clauses in the foreach statement are also available wherever any expression may appear, in the form of a query expression.