| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
<html> |
| 16 |
<head> |
| 17 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 18 |
<meta http-equiv="Content-Language" content="en-us"> |
| 19 |
<title>for</title> |
| 20 |
</head> |
| 21 |
<body bgcolor="#DAD3C5" vlink="#0094D2" link="#003258"> |
| 22 |
<table align="left" border="0" cellspacing="0" cellpadding="0" valign=top> |
| 23 |
<tr> |
| 24 |
<td width=105 height=40><a href="https://projects.eclipse.org/projects/tools.titan"><img src="../images/titan_transparent.gif" border=0 width=105 height=40 align="left" alt="Titan"></a></td> |
| 25 |
</tr> |
| 26 |
</table> |
| 27 |
<table border="0" align="right" cellpadding="0" cellspacing="0"> |
| 28 |
<tr> |
| 29 |
<td><a href="../titan_main.html" alt="contents"><img border="0" src="../images/ao.jpg" width="53" height="40"></a></td> |
| 30 |
<td><a href="../titan_index.html" alt="index"><img border="0" src="../images/up.jpg" width="53" height="40"></a></td> |
| 31 |
<td><a href="float2str.html" alt="previous"><img border="0" src="../images/left.jpg" width="53" height="40"></a></td> |
| 32 |
<td><a href="friend.html" alt="next"><img border="0" src="../images/right.jpg" width="53" height="40"></a></td> |
| 33 |
</tr> |
| 34 |
</table> |
| 35 |
<p><br clear="all"> |
| 36 |
</p> |
| 37 |
<hr> |
| 38 |
<h1>for</h1> |
| 39 |
<hr align="left" width="75%"> |
| 40 |
<p>The keyword defines a counter loop. The value of the index variable is increased, decreased or manipulated in such a way that after a certain number of execution loops a termination criterion |
| 41 |
is reached.</p> |
| 42 |
<ul> |
| 43 |
<li>The index variable can be declared in the statement header. By doing so, the scope of the index variable will be limited to the loop.</li> |
| 44 |
</ul> |
| 45 |
<hr align="left" width="50%"> |
| 46 |
<div align="center"> |
| 47 |
<center> |
| 48 |
<table border="0" width="90%" bgcolor="#FFB599" cellpadding="4"> |
| 49 |
<tr> |
| 50 |
<td width="100%"> |
| 51 |
<h3 align="center"><font face="Courier New" color="#003258" size="5"><b>for (</b></font>[ <i>index_variable_declaration</i> ]<i> initial_value</i><font face="Courier New" color="#003258" |
| 52 |
size="5"><b>;</b></font><i> condition</i><font face="Courier New" color="#003258" size="5"><b>;</b></font><i> manipulator_expression</i><font face="Courier New" color="#003258" size="5"><b>) |
| 53 |
{</b></font> <i> statement_block </i><font face="Courier New" color="#003258" size="5"><b>};</b></font></h3> |
| 54 |
</td> |
| 55 |
</tr> |
| 56 |
</table> |
| 57 |
</center> |
| 58 |
</div> |
| 59 |
<ul> |
| 60 |
<li> |
| 61 |
<p>The <font face="Courier New" color="#003258" size="4"><b>for</b></font> keyword introduces the loop.</p> |
| 62 |
</li> |
| 63 |
<li> |
| 64 |
<p><i>index_variable_declaration</i> is optional. When used, the variable will only be visible within the loop.</p> |
| 65 |
</li> |
| 66 |
<li> |
| 67 |
<p><i>initial_value</i> is used to initialize the index (or counter) variable of the loop.</p> |
| 68 |
</li> |
| 69 |
<li> |
| 70 |
<p><i>condition</i>is a Boolean expression. It is checked at the beginning of each new loop iteration. If it evaluates to false, the loop terminates.</p> |
| 71 |
</li> |
| 72 |
<li> |
| 73 |
<p><i>manipulator_expression</i> is stepping the index variable after each new loop iteration.</p> |
| 74 |
</li> |
| 75 |
<li> |
| 76 |
<p><i>statement_block</i> contains one or more statements. The block will be executed in a loop until <i>condition</i> is true.</p> |
| 77 |
</li> |
| 78 |
</ul> |
| 79 |
<hr align="left" width="50%"> |
| 80 |
<p>Example 1: |
| 81 |
<p><font face="Courier New">var integer j; <br> |
| 82 |
for (j:=1; j<=10; j:= j+2) { log ("index variable: ", j) }<br> |
| 83 |
</font> |
| 84 |
<p>The loop variable j is declared in the first line. In the second line, a loop is defined. The initial value of the index variable (j) is 1. The index variable is increased by 2 every time the |
| 85 |
loop was executed. The loop execution terminates when the index value will have a value greater than 10. The statement block contains a log instruction, which will be executed 5 times resulting in |
| 86 |
five lines in the log.</p> |
| 87 |
<hr align="left" width="25%"> |
| 88 |
<p>Example 2: |
| 89 |
<p><font face="Courier New">for (var integer jj:=1; jj<=10; jj:= jj+2) { log ("index variable: ", jj) }<br> |
| 90 |
</font> |
| 91 |
<p>The only difference to the first example is that the index variable (jj) now is declared within the loop. It means that it will not be visible outside the loop.</p> |
| 92 |
<hr align="left" width="25%"> |
| 93 |
<hr align="left" width="25%"> |
| 94 |
<p><a HREF="BNF.html#forstatement">BNF definition</a> of <font face="Courier New">for</font></p> |
| 95 |
</body> |
| 96 |
</html> |