Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/spirit/phoenix/doc/statements.html @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

File size: 13.8 KB
Line 
1<html>
2<head>
3<!-- Generated by the Spirit (http://spirit.sf.net) QuickDoc -->
4<title>Statements</title>
5<link rel="stylesheet" href="theme/style.css" type="text/css">
6<link rel="prev" href="operators.html">
7<link rel="next" href="binders.html">
8</head>
9<body>
10<table width="100%" height="48" border="0" background="theme/bkd2.gif" cellspacing="2">
11  <tr>
12    <td width="10">
13    </td>
14    <td width="85%">
15      <font size="6" face="Verdana, Arial, Helvetica, sans-serif"><b>Statements</b></font>
16    </td>
17    <td width="112"><a href="http://spirit.sf.net"><img src="theme/spirit.gif" align="right" border="0"></a></td>
18  </tr>
19</table>
20<br>
21<table border="0">
22  <tr>
23    <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
24    <td width="30"><a href="operators.html"><img src="theme/l_arr.gif" border="0"></a></td>
25    <td width="20"><a href="binders.html"><img src="theme/r_arr.gif" border="0"></a></td>
26   </tr>
27</table>
28<a name="lazy_statements"></a><h2>Lazy statements</h2><p>
29The primitives and composite building blocks presented before are sufficiently powerful to construct quite elaborate structures and facilities. We have presented lazy-functions and lazy-operators. How about lazy-statements? First, an appetizer:</p>
30<p>
31Print all odd-numbered contents of an STL container using std::for_each (sample4.cpp):</p>
32<code><pre>
33    <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>(),
34        </span><span class=identifier>if_</span><span class=special>(</span><span class=identifier>arg1 </span><span class=special>% </span><span class=number>2 </span><span class=special>== </span><span class=number>1</span><span class=special>)
35        [
36            </span><span class=identifier>cout </span><span class=special>&lt;&lt; </span><span class=identifier>arg1 </span><span class=special>&lt;&lt; </span><span class=literal>' '
37        </span><span class=special>]
38    );
39</span></pre></code>
40<p>
41Huh? Is that valid C++? Read on...</p>
42<p>
43Yes, it is valid C++. The sample code above is as close as you can get to the syntax of C++. This stylized C++ syntax differs from actual C++ code. First, the if has a trailing underscore. Second, the block uses square brackets  instead of the familiar curly braces {}.</p>
44<p>
45Here are more examples with annotations. The code almost speaks for itself.</p>
46<p>
47<b>1) block statement:</b></p>
48<code><pre>
49    <span class=identifier>statement</span><span class=special>,
50    </span><span class=identifier>statement</span><span class=special>,
51    ....
52    </span><span class=identifier>statement
53</span></pre></code>
54<p>
55Basically, these are comma separated statements. Take note that unlike the C/C++ semicolon, the comma is a separator put *in-between* statements. This is like Pascal's semicolon separator, rather than C/C++'s semicolon terminator. For example:</p>
56<code><pre>
57    <span class=identifier>statement</span><span class=special>,
58    </span><span class=identifier>statement</span><span class=special>,
59    </span><span class=identifier>statement</span><span class=special>,     //  </span><span class=identifier>ERROR</span><span class=special>!
60</span></pre></code>
61<p>
62Is an error. The last statement should not have a comma. Block statements can be grouped using the parentheses. Again, the last statement in a group should not have a trailing comma.</p>
63<code><pre>
64    <span class=identifier>statement</span><span class=special>,
65    </span><span class=identifier>statement</span><span class=special>,
66    (
67        </span><span class=identifier>statement</span><span class=special>,
68        </span><span class=identifier>statement
69    </span><span class=special>),
70    </span><span class=identifier>statement
71</span></pre></code>
72<p>
73Outside the square brackets, block statements should be grouped. For example:</p>
74<code><pre>
75    <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>(),
76        (
77            </span><span class=identifier>do_this</span><span class=special>(</span><span class=identifier>arg1</span><span class=special>),
78            </span><span class=identifier>do_that</span><span class=special>(</span><span class=identifier>arg1</span><span class=special>)
79        )
80    );
81</span></pre></code>
82<p>
83<b>2) if_ statement:</b></p>
84<p>
85We have seen the if_ statement. The syntax is:</p>
86<code><pre>
87    <span class=identifier>if_</span><span class=special>(</span><span class=identifier>conditional_expression</span><span class=special>)
88    [
89        </span><span class=identifier>sequenced_statements
90    </span><span class=special>]
91</span></pre></code>
92<p>
93<b>3) if_ else_ statement:</b></p>
94<p>
95The syntax is</p>
96<code><pre>
97    <span class=identifier>if_</span><span class=special>(</span><span class=identifier>conditional_expression</span><span class=special>)
98    [
99        </span><span class=identifier>sequenced_statements
100    </span><span class=special>]
101    .</span><span class=identifier>else_
102    </span><span class=special>[
103        </span><span class=identifier>sequenced_statements
104    </span><span class=special>]
105</span></pre></code>
106<p>
107Take note that else has a prefix dot and a trailing underscore: .else_</p>
108<p>
109Example: This code prints out all the elements and appends &quot; &gt; 5&quot;, &quot; == 5&quot; or &quot; &lt; 5&quot; depending on the element's actual value:</p>
110<code><pre>
111    <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>(),
112        </span><span class=identifier>if_</span><span class=special>(</span><span class=identifier>arg1 </span><span class=special>&gt; </span><span class=number>5</span><span class=special>)
113        [
114            </span><span class=identifier>cout </span><span class=special>&lt;&lt; </span><span class=identifier>arg1 </span><span class=special>&lt;&lt; </span><span class=string>&quot; &gt; 5\n&quot;
115        </span><span class=special>]
116        .</span><span class=identifier>else_
117        </span><span class=special>[
118            </span><span class=identifier>if_</span><span class=special>(</span><span class=identifier>arg1 </span><span class=special>== </span><span class=number>5</span><span class=special>)
119            [
120                </span><span class=identifier>cout </span><span class=special>&lt;&lt; </span><span class=identifier>arg1 </span><span class=special>&lt;&lt; </span><span class=string>&quot; == 5\n&quot;
121            </span><span class=special>]
122            .</span><span class=identifier>else_
123            </span><span class=special>[
124                </span><span class=identifier>cout </span><span class=special>&lt;&lt; </span><span class=identifier>arg1 </span><span class=special>&lt;&lt; </span><span class=string>&quot; &lt; 5\n&quot;
125            </span><span class=special>]
126        ]
127    );
128</span></pre></code>
129<p>
130Notice how the if_ else_ statement is nested.</p>
131<p>
132<b>4) while_ statement:</b></p>
133<p>
134The syntax is:</p>
135<code><pre>
136    <span class=identifier>while_</span><span class=special>(</span><span class=identifier>conditional_expression</span><span class=special>)
137    [
138        </span><span class=identifier>sequenced_statements
139    </span><span class=special>]
140</span></pre></code>
141<p>
142Example: This code decrements each element until it reaches zero and prints out the number at each step. A newline terminates the printout of each value.</p>
143<code><pre>
144    <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>(),
145        (
146            </span><span class=identifier>while_</span><span class=special>(</span><span class=identifier>arg1</span><span class=special>--)
147            [
148                </span><span class=identifier>cout </span><span class=special>&lt;&lt; </span><span class=identifier>arg1 </span><span class=special>&lt;&lt; </span><span class=string>&quot;, &quot;
149            </span><span class=special>],
150            </span><span class=identifier>cout </span><span class=special>&lt;&lt; </span><span class=identifier>val</span><span class=special>(</span><span class=string>&quot;\n&quot;</span><span class=special>)
151        )
152    );
153</span></pre></code>
154<p>
155<b>5) do_ while_ statement:</b></p>
156<p>
157The syntax is:</p>
158<code><pre>
159    <span class=identifier>do_
160    </span><span class=special>[
161        </span><span class=identifier>sequenced_statements
162    </span><span class=special>]
163    .</span><span class=identifier>while_</span><span class=special>(</span><span class=identifier>conditional_expression</span><span class=special>)
164</span></pre></code>
165<p>
166Again, take note that while has a prefix dot and a trailing underscore: .while_</p>
167<p>
168Example: This code is almost the same as the previous example above with a slight twist in logic.</p>
169<code><pre>
170    <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>(),
171        (
172            </span><span class=identifier>do_
173            </span><span class=special>[
174                </span><span class=identifier>cout </span><span class=special>&lt;&lt; </span><span class=identifier>arg1 </span><span class=special>&lt;&lt; </span><span class=string>&quot;, &quot;
175            </span><span class=special>]
176            .</span><span class=identifier>while_</span><span class=special>(</span><span class=identifier>arg1</span><span class=special>--),
177            </span><span class=identifier>cout </span><span class=special>&lt;&lt; </span><span class=identifier>val</span><span class=special>(</span><span class=string>&quot;\n&quot;</span><span class=special>)
178        )
179    );
180</span></pre></code>
181<p>
182<b>6) for_ statement:</b></p>
183<p>
184The syntax is:</p>
185<code><pre>
186    <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>)
187    [
188        </span><span class=identifier>sequenced_statements
189    </span><span class=special>]
190</span></pre></code>
191<p>
192It is again almost similar to 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. for_(,,) is invalid).</p>
193<p>
194Example: This code prints each element N times where N is the element's value. A newline terminates the printout of each value.</p>
195<code><pre>
196    <span class=keyword>int </span><span class=identifier>iii</span><span class=special>;
197    </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>(),
198        (
199            </span><span class=identifier>for_</span><span class=special>(</span><span class=identifier>var</span><span class=special>(</span><span class=identifier>iii</span><span class=special>) = </span><span class=number>0</span><span class=special>, </span><span class=identifier>var</span><span class=special>(</span><span class=identifier>iii</span><span class=special>) &lt; </span><span class=identifier>arg1</span><span class=special>, ++</span><span class=identifier>var</span><span class=special>(</span><span class=identifier>iii</span><span class=special>))
200            [
201                </span><span class=identifier>cout </span><span class=special>&lt;&lt; </span><span class=identifier>arg1 </span><span class=special>&lt;&lt; </span><span class=string>&quot;, &quot;
202            </span><span class=special>],
203            </span><span class=identifier>cout </span><span class=special>&lt;&lt; </span><span class=identifier>val</span><span class=special>(</span><span class=string>&quot;\n&quot;</span><span class=special>)
204        )
205    );
206</span></pre></code>
207<p>
208As 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 for_each, what was passed is just a functor, no more, no less.</p>
209<table width="80%" border="0" align="center">
210  <tr>
211    <td class="note_box">
212<img src="theme/note.gif"></img> Unlike lazy functions and lazy operators, lazy statements always return void.    </td>
213  </tr>
214</table>
215<table border="0">
216  <tr>
217    <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
218    <td width="30"><a href="operators.html"><img src="theme/l_arr.gif" border="0"></a></td>
219    <td width="20"><a href="binders.html"><img src="theme/r_arr.gif" border="0"></a></td>
220   </tr>
221</table>
222<br>
223<hr size="1">
224<p class="copyright">Copyright &copy; 2001-2002 Joel de Guzman<br>
225  <br>
226<font size="2">Use, modification and distribution is subject to the Boost Software
227    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
228    http://www.boost.org/LICENSE_1_0.txt) </font> </p>
229</body>
230</html>
Note: See TracBrowser for help on using the repository browser.