<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../style.css">
<title>
Gambas Documentation - FOR EACH
</title>
</head>
<table class="none" width="100%" cellpadding="0" cellspacing="0" border="0">
<tr><td align="left">
<font size="-1">
<a href="../../help+en"><img class="flag" alt="Home" border="0" src="../../img/lang/en.png" align="center"></a>&nbsp;
<a href="../lang+en">Up</a>&nbsp;
<a href="for+en">Previous</a>&nbsp;
<a href="format+en">Next</a>&nbsp;
</td></tr></table>
<div class="notab">
<h1>
FOR EACH
</h1>
<div class="black"><font size="-2"><b>Syntax</b></font></div>
<pre class="syntax"><b>FOR EACH</b> <u>Variable</u> <b>IN</b> <u>Expression</u>
&nbsp;&nbsp;...
<b>NEXT</b></pre><p>

Repeats a loop while enumerating an object.
<p>
<u>Expression</u>  must be a reference to an enumerable object: for example, a collection, or an array.
<p>
<div class="gray"><font size="-2"><b>Example</b></font></div>
<pre class="example">DIM Dict AS NEW Collection
DIM Element AS String

Dict[&quot;Blue&quot;] = 3
Dict[&quot;Red&quot;] = 1
Dict[&quot;Green&quot;] = 2

FOR EACH Element IN Dict
  PRINT Element;
NEXT
<hr>3 1 2</pre>
<p>
<div class="black"><font size="-2"><b>Syntax</b></font></div>
<pre class="syntax"><b>FOR EACH</b> <u>Expression</u>
&nbsp;&nbsp;...
<b>NEXT</b></pre><p>

This syntax must be used when <u>Expression</u> is a enumerable object that is not a real container: for example, the result of a database query.
<p>
<div class="gray"><font size="-2"><b>Example</b></font></div>
<pre class="example">DIM Res AS Result

Res = DB.Exec(&quot;SELECT * FROM MyTable&quot;)

FOR EACH Res
  PRINT Res!Code; &quot; &quot;; Res!Name
NEXT</pre>
<p>
<div class="warning"><table class="none" border="0"><tr><td width="40" valign="top"><img border="0" src="../../img/warning.png" align="center"></td><td valign="top">
The order of the enumeration in not necessarily predictable. See the documentation of each enumerable class for more details on that.
</td></tr></table></div>
<p>
<hr><b>See also</b><br>
<a href="../cat/loop+en">Loop Control Structures</a>&nbsp; <a href="../cat/container+en">Native Container Classes</a>&nbsp;

</div>
</body>
</html>

