| <html> |
| <head> |
| <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> |
| <title>for_ Statement</title> |
| <link rel="stylesheet" href="../../../boostbook.css" type="text/css"> |
| <meta name="generator" content="DocBook XSL Stylesheets V1.76.1"> |
| <link rel="home" href="../../../index.html" title="Chapter 1. Phoenix 3.0"> |
| <link rel="up" href="../statement.html" title="Statement"> |
| <link rel="prev" href="___do_while_____statement.html" title="do_while_ Statement"> |
| <link rel="next" href="try__catch__statement.html" title="try_ catch_ Statement"> |
| </head> |
| <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> |
| <table cellpadding="2" width="100%"><tr><td valign="top"></td></tr></table> |
| <hr> |
| <div class="spirit-nav"> |
| <a accesskey="p" href="___do_while_____statement.html"><img src="../../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../statement.html"><img src="../../../images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="try__catch__statement.html"><img src="../../../images/next.png" alt="Next"></a> |
| </div> |
| <div class="section"> |
| <div class="titlepage"><div><div><h4 class="title"> |
| <a name="phoenix.modules.statement.for__statement"></a><a class="link" href="for__statement.html" title="for_ Statement">for_ Statement</a> |
| </h4></div></div></div> |
| <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">phoenix</span><span class="special">/</span><span class="identifier">statement</span><span class="special">/</span><span class="keyword">for</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> |
| </pre> |
| <p> |
| The syntax is: |
| </p> |
| <pre class="programlisting"><span class="identifier">for_</span><span class="special">(</span><span class="identifier">init_statement</span><span class="special">,</span> <span class="identifier">conditional_expression</span><span class="special">,</span> <span class="identifier">step_statement</span><span class="special">)</span> |
| <span class="special">[</span> |
| <span class="identifier">sequenced_statements</span> |
| <span class="special">]</span> |
| </pre> |
| <p> |
| It is again very similar to the C++ for statement. Take note that the init_statement, |
| conditional_expression and step_statement are separated by the comma instead of the semi-colon |
| and each must be present (i.e. <code class="computeroutput"><span class="identifier">for_</span><span class="special">(,,)</span></code> is invalid). This is a case where the |
| <a class="link" href="../core/nothing.html" title="Nothing"><code class="computeroutput"><span class="identifier">nothing</span></code></a> |
| actor can be useful. |
| </p> |
| <p> |
| Example: This code prints each element N times where N is the element's |
| value. A newline terminates the printout of each value. |
| </p> |
| <pre class="programlisting"><span class="keyword">int</span> <span class="identifier">iii</span><span class="special">;</span> |
| <span class="identifier">std</span><span class="special">::</span><span class="identifier">for_each</span><span class="special">(</span><span class="identifier">c</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">c</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span> |
| <span class="special">(</span> |
| <span class="identifier">for_</span><span class="special">(</span><span class="identifier">ref</span><span class="special">(</span><span class="identifier">iii</span><span class="special">)</span> <span class="special">=</span> <span class="number">0</span><span class="special">,</span> <span class="identifier">ref</span><span class="special">(</span><span class="identifier">iii</span><span class="special">)</span> <span class="special"><</span> <span class="identifier">arg1</span><span class="special">,</span> <span class="special">++</span><span class="identifier">ref</span><span class="special">(</span><span class="identifier">iii</span><span class="special">))</span> |
| <span class="special">[</span> |
| <span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">arg1</span> <span class="special"><<</span> <span class="string">", "</span> |
| <span class="special">],</span> |
| <span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">val</span><span class="special">(</span><span class="string">"\n"</span><span class="special">)</span> |
| <span class="special">)</span> |
| <span class="special">);</span> |
| </pre> |
| <p> |
| As before, all these are lazily evaluated. The result of such statements |
| are in fact composites that are passed on to STL's for_each function. In |
| the viewpoint of <code class="computeroutput"><span class="identifier">for_each</span></code>, |
| what was passed is just a functor, no more, no less. |
| </p> |
| </div> |
| <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> |
| <td align="left"></td> |
| <td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2010 Joel de Guzman, Dan Marsden, Thomas Heller<p> |
| Distributed under the Boost Software License, Version 1.0. (See accompanying |
| file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) |
| </p> |
| </div></td> |
| </tr></table> |
| <hr> |
| <div class="spirit-nav"> |
| <a accesskey="p" href="___do_while_____statement.html"><img src="../../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../statement.html"><img src="../../../images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="try__catch__statement.html"><img src="../../../images/next.png" alt="Next"></a> |
| </div> |
| </body> |
| </html> |