| 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>select</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="runs.html" alt="previous"><img border="0" src="../images/left.jpg" width="53" height="40"></a></td> |
| 32 |
<td><a href="self.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>select</h1> |
| 39 |
<p>The <code>select</code> is a conditional statement that is used to branch program execution based on the value of an expression. The expression is evaluated and the resulting value is matched |
| 40 |
against templates specified within the <code>case</code>s. The execution continues with the statement block of the <code>case</code> matching first. |
| 41 |
<p>Unlike to the <code>if</code> statement where the way forward is determined with boolean conditions, in the <code>select</code> statement the branching is determined using template matching. |
| 42 |
<p>It is important to mention that although the <code>select</code> statement resembles to the <code>alt</code> statement, these differ significantly. The evaluation of the <code>select</code> |
| 43 |
statement does not involve snapshot handling thus it never blocks execution. |
| 44 |
<p>Related keywords:</p> |
| 45 |
<ul> |
| 46 |
<li><a href="alt.html"><b><font face="Courier New" color="#003258" size="4">alt</font></b></a></li> |
| 47 |
<li><a href="else.html"><b><font face="Courier New" color="#003258" size="4">else</font></b></a></li> |
| 48 |
<li><a href="if.html"><b><font face="Courier New" color="#003258" size="4">if</font></b></a></li> |
| 49 |
<li><a href="match.html"><b><font face="Courier New" color="#003258" size="4">match</font></b></a></li> |
| 50 |
<li><a href="template.html"><b><font face="Courier New" color="#003258" size="4">template</font></b></a></li> |
| 51 |
</ul> |
| 52 |
<hr align="left" width="75%"> |
| 53 |
<div align="center"> |
| 54 |
<center> |
| 55 |
<table border="0" width="90%" bgcolor="#FFB599" cellpadding="4"> |
| 56 |
<tr> |
| 57 |
<td width="100%"> |
| 58 |
<h3 align="center"><font face="Courier New" color="#003258" size="5"><b>select (</b></font><i> expression</i><font face="Courier New" color="#003258" size="5"><b>){</b></font> <i> |
| 59 |
cases </i><font face="Courier New" color="#003258" size="5"><b>};</b></font></h3> |
| 60 |
</td> |
| 61 |
</tr> |
| 62 |
</table> |
| 63 |
</center> |
| 64 |
</div> |
| 65 |
<ul> |
| 66 |
<li> |
| 67 |
<p>The <font face="Courier New" color="#003258" size="4"><b>select</b></font> keyword introduces the conditional branch.</p> |
| 68 |
</li> |
| 69 |
<li> |
| 70 |
<p><i>expression</i> shall evaluate to a specific value.</p> |
| 71 |
</li> |
| 72 |
<li> |
| 73 |
<p><i>cases</i> contains one or more cases. |
| 74 |
</ul> |
| 75 |
<div align="center"> |
| 76 |
<center> |
| 77 |
<table border="0" width="90%" bgcolor="#FFB599" cellpadding="4"> |
| 78 |
<tr> |
| 79 |
<td width="100%"> |
| 80 |
<h3 align="center"><font face="Courier New" color="#003258" size="5"><b>case (</b></font><i> matchings </i><font face="Courier New" color="#003258" size="5"><b>){</b></font> <i> |
| 81 |
statement_block </i> <font face="Courier New" color="#003258" size="5"><b>}; </b></font></h3> |
| 82 |
</td> |
| 83 |
</tr> |
| 84 |
</table> |
| 85 |
</center> |
| 86 |
</div> |
| 87 |
<ul> |
| 88 |
<li> |
| 89 |
<p>The <font face="Courier New" color="#003258" size="4"><b>case</b></font> keyword introduces the next case |
| 90 |
<li> |
| 91 |
<p><i>matchings</i> is a comma separated list of template references (including inline templates) |
| 92 |
<li> |
| 93 |
<p><i>statement_block</i> contains one or more statements. The block will only be executed if the value of select expression matches at least one template of this case. |
| 94 |
</ul> |
| 95 |
<div align="center"> |
| 96 |
<center> |
| 97 |
<table border="0" width="90%" bgcolor="#FFB599" cellpadding="4"> |
| 98 |
<tr> |
| 99 |
<td width="100%"> |
| 100 |
<h3 align="center"><font face="Courier New" color="#003258" size="5"><b> case else {</b></font> <i> statement_block </i> <font face="Courier New" color="#003258" size="5"><b>}; |
| 101 |
</b></font></h3> |
| 102 |
</td> |
| 103 |
</tr> |
| 104 |
</table> |
| 105 |
</center> |
| 106 |
</div> |
| 107 |
<ul> |
| 108 |
<li> |
| 109 |
<p>The <font face="Courier New" color="#003258" size="4"><b>case else</b></font> may be used to specify the statements to be executed when no cases match. |
| 110 |
<li>When none of the <code>case</code>s match and there is no <code>case else</code> in the given <code>select</code> statement then none of the statement blocks will be executed. |
| 111 |
</ul> |
| 112 |
<hr align="left" width="50%"> |
| 113 |
<p><a href="#Ex3">Examples:</a></p> |
| 114 |
<hr align="left" width="50%"> |
| 115 |
<p><a name="Ex4">Example 1</a>: |
| 116 |
<p><code> select (v) {<br> |
| 117 |
case (0) { log("zero") }<br> |
| 118 |
case ( (-infinity..-1) ) { log("negative") }<br> |
| 119 |
case ( (1..infinity) ) { log("positive") }<br> |
| 120 |
} </code> |
| 121 |
<p>The above select statement performs sign check using inline value range templates and logs the result. |
| 122 |
<p><a name="Ex5">Example 2</a>: |
| 123 |
<p><code> select (myMessage) {<br> |
| 124 |
case (tr_Message1, tr_Message2, tr_Message3) { log("1st") }<br> |
| 125 |
case (tr_MessageA, tr_MessageB) { log("2nd") }<br> |
| 126 |
case else { log("none") }<br> |
| 127 |
} </code> |
| 128 |
<p>When the message stored in <code>myMessage</code> variable matches templates <code>tr_Message1</code>, <code>tr_Message2</code> or <code>tr_Message3</code> then the statement block of the first |
| 129 |
case is executed. If <code>myMessage</code> matches <code>tr_MessageA</code> or <code>tr_MessageB</code> then the second statement block is executed. Otherwise, when none of the enlisted templates |
| 130 |
match <code>myMessage</code> then the statement block of the <code>case else</code> branch is executed. |
| 131 |
<hr align="left" width="25%"> |
| 132 |
<p><a HREF="BNF.html#conditionalconstruct">BNF definition</a> of <font face="Courier New"> select</font></p> |
| 133 |
</body> |
| 134 |
</html> |