Skip to content
Snippets Groups Projects
Commit c216ba2d authored by Chris Angelico's avatar Chris Angelico Committed by Henrik (Grubba) Grubbström
Browse files

Document the three-arg form of foreach

parent 0976b6e8
No related branches found
No related tags found
No related merge requests found
...@@ -253,7 +253,7 @@ this:</p> ...@@ -253,7 +253,7 @@ this:</p>
<example> <example>
foreach ( array_expression, variable ) foreach ( array_expression, variable )
statement ; statement;
</example> </example>
<p>We have already seen an example of <tt>foreach</tt> in the <tt>find_song</tt> <p>We have already seen an example of <tt>foreach</tt> in the <tt>find_song</tt>
...@@ -273,7 +273,7 @@ function in chapter 2. What foreach does is:</p> ...@@ -273,7 +273,7 @@ function in chapter 2. What foreach does is:</p>
doing the same thing with a <tt>for</tt> loop, as shown here:</p> doing the same thing with a <tt>for</tt> loop, as shown here:</p>
<example> <example>
array tmp1= array_expression; array tmp1 = array_expression;
for ( tmp2 = 0; tmp2 &lt; sizeof(tmp1); tmp2++ ) for ( tmp2 = 0; tmp2 &lt; sizeof(tmp1); tmp2++ )
{ {
variable = tmp1 [ tmp2 ]; variable = tmp1 [ tmp2 ];
...@@ -281,6 +281,27 @@ for ( tmp2 = 0; tmp2 &lt; sizeof(tmp1); tmp2++ ) ...@@ -281,6 +281,27 @@ for ( tmp2 = 0; tmp2 &lt; sizeof(tmp1); tmp2++ )
} }
</example> </example>
<p><tt>Foreach</tt> can also iterate over a mapping, providing both the key and the
value at the same time:</p>
<example>
foreach ( mapping_expression; variable1; variable2 )
statement;
</example>
<p>This is approximately equivalent to:</p>
<example>
mapping tmp1 = mapping_expression;
array tmp2 = indices(tmp1);
for ( tmp3 = 0; tmp3 &lt; sizeof(tmp2); tmp3++ )
{
variable1 = tmp2 [ tmp3 ];
variable2 = tmp1 [ variable1 ];
statement;
}
</example>
</subsection> </subsection>
</section> </section>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment