Virtools脚本语言(VSL)教程 - 表达式与运算符
<h3>表达式</h3>
<p>表达式是变量、运算符的任意组合,且表达式返回一个单一值。值可以是数字、字符串或逻辑值。</p>
<p>有两种类型的表达式:有些是将值赋给变量,有些则是简单的有一个值。比如表达式 x = 5 用了赋值运算符来将值5赋给x。x = 5 的赋值过程也返回值5。而表达式( 4 + 1)则简单地返回5,它没有执行赋值。</p>
<h3>
<a name="TOC-2"></a>运算符</h3>
<p>VSL中有一些类型的运算符:</p>
<blockquote>
<li>赋值运算符</li>
<li>比较运算符</li>
<li>算术运算符</li>
<li>逻辑运算符</li>
</blockquote>
<h4>
<a name="TOC-3"></a>赋值运算符</h4>
<p>赋值运算符将值赋给它左边的运算对象(operand),基于它右边运算对象的值。<br>等号 (=) 是赋值运算符的基,x = y 意味着将y的值赋给x。</p>
<h5>
<a name="TOC-4"></a>支持的</h5>
<table style="width: 400px;" border="1" cellspacing="0" cellpadding="1"><tbody>
<tr>
<td width="45%" valign="top"><strong>运算符</strong></td>
<td width="55%" valign="top">
<strong>意义</strong><br>
</td>
</tr>
<tr>
<td width="45%" valign="top">x += y</td>
<td width="55%" valign="top">x = x + y</td>
</tr>
<tr>
<td width="45%" valign="top">x -= y</td>
<td width="55%" valign="top">x = x - y</td>
</tr>
<tr>
<td width="45%" valign="top">x *= y</td>
<td width="55%" valign="top">x = x * y</td>
</tr>
<tr>
<td width="45%" valign="top">x /= y</td>
<td width="55%" valign="top">x = x / y</td>
</tr>
</tbody></table>
<h5>
<a name="TOC-5"></a>不支持的</h5>
<table style="width: 180px;" border="1" cellspacing="0" cellpadding="1"><tbody>
<tr>
<td valign="top"><strong>运算符</strong></td>
</tr>
<tr>
<td valign="top">x %= y</td>
</tr>
<tr>
<td valign="top">x <<= y</td>
</tr>
<tr>
<td valign="top">x >>= y</td>
</tr>
<tr>
<td valign="top">x >>>= y</td>
</tr>
<tr>
<td valign="top">x &= y</td>
</tr>
<tr>
<td valign="top">x ^= y</td>
</tr>
<tr>
<td valign="top">x |= y</td>
</tr>
</tbody></table>
<p>实例:</p>
<pre>void main()<br>{<br> int x = 2;<br> int y = 4;<br><br> x += y; <span>// x is equal to 6</span>
x -= 3; <span>// x is equal to 3</span>
x *= y-1; <span>// x is equal to 9</span>
x /= x-y; <span>// x is equal to 1</span>
}
<p><strong>NOTE</strong>赋值运算符表达式在赋值完成后返回它们左边运算对象的值。因此,以下脚本从语法角度是有效的:</p>
<pre>void main()<br>{<br> int x;<br> int y;<br> int z = 10;<br><br> x = y = z; <span>// ok, x and y equal to 10</span>
x += y += z; <span>// ok, x equal to 30 and y to 20</span>
x *= y /= z; <span>// ok. x equal to 60 and y to 2</span>
<span>// ...</span>
}
<h4>
<a name="TOC-6"></a>比较运算符</h4>
<p>比较运算符对它的运算对象进行比较,并返回一个逻辑值,基于比较结果是否为真。这里是VSL支持的比较运算符。</p>
</pre></pre>
<table style="width: 700px;" border="1" cellspacing="0" cellpadding="1"><tbody>
<tr>
<td width="25%" valign="top">
<strong>运算符</strong><br>
</td>
<td width="31%" valign="top">
<strong>意义</strong><br>
</td>
<td width="44%" valign="top"><strong>说明</strong></td>
</tr>
<tr>
<td width="25%" valign="top">==</td>
<td width="31%" valign="top">相等</td>
<td width="44%" valign="top">如果运算对象相等,返回true。</td>
</tr>
<tr>
<td width="25%" valign="top">!=</td>
<td width="31%" valign="top">不等</td>
<td width="44%" valign="top">如果运算对象不相等,返回true。</td>
</tr>
<tr>
<td width="25%" valign="top">></td>
<td width="31%" valign="top">大于</td>
<td width="44%" valign="top">如果左边运算对象大于右边的,返回true。</td>
</tr>
<tr>
<td width="25%" valign="top">>=</td>
<td width="31%" valign="top">大于等于</td>
<td width="44%" valign="top">如果左边运算对象大于或等于右边的,返回true。</td>
</tr>
<tr>
<td width="25%" valign="top"><</td>
<td width="31%" valign="top">小于</td>
<td width="44%" valign="top">如果左边运算对象小于右边的,返回true。</td>
</tr>
<tr>
<td width="25%" valign="top"><=</td>
<td width="31%" valign="top">小于等于</td>
<td width="44%" valign="top">如果左边运算对象小于或等于右边的,返回true。</td>
</tr>
</tbody></table>
<p>实例:</p>
<pre>void main()<br>{<br> int i = 10;<br> float f = 2.3;<br> bool b1 = i > f; <span>// b1 equal to true</span>
bool b2 = b1 == i; <span>// b2 equal to false</span>
i = b1 != b2; <span>// i equal to 1;</span>
}
<h4>
<a name="TOC-7"></a>算术运算符</h4>
<p>算术运算符将数值作为它们的运算对象并返回一单个数值。</p>
</pre>
<table style="width: 700px;" border="1" cellspacing="0" cellpadding="1"><tbody>
<tr>
<td width="25%" valign="top">
<strong>运算符</strong><br>
</td>
<td width="31%" valign="top"><strong>意义</strong></td>
<td width="44%" valign="top"><strong>描述</strong></td>
</tr>
<tr>
<td width="25%" valign="top">+</td>
<td width="31%" valign="top">加法</td>
<td width="44%" valign="top">二元运算符。让它的两个运算对象相加。运算对象可以为整型或浮点型。</td>
</tr>
<tr>
<td width="25%" valign="top">-</td>
<td width="31%" valign="top">减法</td>
<td width="44%" valign="top">二元运算符。将第一个运算对象减去第二个运算对象。可以为整型或浮点型。</td>
</tr>
<tr>
<td width="25%" valign="top">*</td>
<td width="31%" valign="top">乘法</td>
<td width="44%" valign="top">二元运算符。让它的两个运算对象相乘。</td>
</tr>
<tr>
<td width="2<mce:script type=">
<script src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" type="text/javascript"></script>
5%" valign="top">/</td>
<td width="31%" valign="top">除法</td>
<td width="44%" valign="top">二元运算符。让第二个运算对象去除第一个运算对象。</td>
</tr>
<tr>
<td width="25%" valign="top">%</td>
<td width="31%" valign="top">取余</td>
<td width="44%" valign="top">二元运算符。返回两个运算对象相除后的整数部分。</td>
</tr>
<tr>
<td width="25%" valign="top">++</td>
<td width="31%" valign="top">增加</td>
<td width="44%" valign="top">一元运算符。将它的运算对象加1。如果是前置运算符(++x),则在加1后返回它的运算对象;如果是后置运算符(x++),则在加1之前返回运算对象的值。</td>
</tr>
<tr>
<td width="25%" valign="top">--</td>
<td width="31%" valign="top">减少</td>
<td width="44%" valign="top">一元运算符。将它的运算对象减1。返回值情况类似增加运算符。</td>
</tr>
<tr>
<td width="25%" valign="top">(Unary) -</td>
<td width="31%" valign="top">一元求负</td>
<td width="44%" valign="top">一元运算符。返回它运算符的负值。</td>
</tr>
</tbody></table>
<p>实例:</p>
<pre>void main()<br>{<br> int i1 = 42;<br> int i2 = 123;<br> float f1 = .2;<br> float f2 = 3567.3623;<br><br> int r1 = i2-i1; <span>// r1 equal 81</span>
float r2 = i2*f1; <span>// r2 equal to 24.6</span>
int r3 = i2/2; <span>// r3 equal to 61</span>
int r4 = f1*f2; <span>// r4 equal to 713</span>
float r5 = f1*f2; <span>/ r5 equal to 713.472473</span>
}
<pre>void main()<br>{<br> int a = 10;<br> a++; <span>// a equal to 11</span>
int b = ++a; <span>// a and b equal to 12</span>
int c = a--; <span>// a equal to 11 and c equal to 12</span>
}
<h4>
<a name="TOC-8"></a>逻辑运算符</h4>
<p>逻辑运算符 与(&&) 和 或 (||),用于将多个关系表达式或等式形式的条件组合起来。</p>
</pre></pre>
<table style="width: 700px;" border="1" cellspacing="0" cellpadding="1"><tbody>
<tr>
<td width="25%" valign="top"><strong>Operator</strong></td>
<td width="31%" valign="top"><strong>Meaning</strong></td>
<td width="44%" valign="top"><strong>Description</strong></td>
</tr>
<tr>
<td width="25%" valign="top">&&</td>
<td width="31%" valign="top">逻辑 与</td>
<td width="44%" valign="top">Returns true if both operands are true, otherwise, returns false.</td>
</tr>
<tr>
<td width="25%" valign="top">||</td>
<td width="31%" valign="top">逻辑 或</td>
<td width="44%" valign="top">Returns true if either operand is true, if both are false, returns false.</td>
</tr>
<tr>
<td width="25%" valign="top">!</td>
<td width="31%" valign="top">逻辑 否</td>
<td width="44%" valign="top">Returns true if operand is false, else returns false.</td>
</tr>
</tbody></table>
<p>实例:</p>
<pre>void main()<br>{<br> Entity3D ent = Entity3D.Cast(bc.GetOwner());<br> if (ent && ent.CanBeHide()) <span>// if ent is null (false), expression ent.CanBeHide() is not evaluated</span>
{
<span>// ...</span>
}
}
</pre>