Angelscript/docs/manual/doc_script_statements.html

248 lines
12 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.18"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>AngelScript: Statements</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function() { init_search(); });
/* @license-end */
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectlogo"><img alt="Logo" src="aslogo_small.png"/></td>
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">AngelScript
</div>
</td>
<td> <div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.18 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('doc_script_statements.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="PageDoc"><div class="header">
<div class="headertitle">
<div class="title">Statements </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><ul>
<li><a class="el" href="doc_script_statements.html#variable">Variable declarations</a></li>
<li><a class="el" href="doc_script_statements.html#expression">Expression statement</a></li>
<li><a class="el" href="doc_script_statements.html#if">Conditions: if / if-else / switch-case</a></li>
<li><a class="el" href="doc_script_statements.html#while">Loops: while / do-while / for</a></li>
<li><a class="el" href="doc_script_statements.html#break">Loop control: break / continue</a></li>
<li><a class="el" href="doc_script_statements.html#return">Return statement</a></li>
<li><a class="el" href="doc_script_statements.html#block">Statement blocks</a></li>
<li><a class="el" href="doc_script_statements.html#try">Try-catch blocks</a></li>
</ul>
<h1><a class="anchor" id="variable"></a>
Variable declarations</h1>
<pre>
int var = 0, var2 = 10;
object@ handle, handle2;
const float pi = 3.141592f;
</pre><p>Variables must be declared before they are used within the statement block, or any sub blocks. When the code exits the statement block where the variable was declared the variable is no longer valid.</p>
<p>A variable can be declared with or without an initial expression. If it is declared with an initial expression it, the expression must have the evaluate to a type compatible with the variable type.</p>
<p>Any number of variables can be declared on the same line separated with commas, where all variables then get the same type.</p>
<p>Variables can be declared as <code>const</code>. In these cases the value of the variable cannot be changed after initialization.</p>
<p>Variables of primitive types that are declared without an initial value, will have a random value. Variables of complex types, such as handles and object are initialized with a default value. For handles this is <code>null</code>, for objects this is what is defined by the object's default constructor.</p>
<h1><a class="anchor" id="expression"></a>
Expression statement</h1>
<pre>
a = b; // a variable assignment
func(); // a function call
</pre><p>Any <a class="el" href="doc_expressions.html">expression</a> may be placed alone on a line as a statement. This will normally be used for variable assignments or function calls that don't return any value of importance.</p>
<p>All expression statements must end with a <code>;</code>.</p>
<h1><a class="anchor" id="if"></a>
Conditions: if / if-else / switch-case</h1>
<pre>
if( condition )
{
// Do something if condition is true
}</pre><pre> if( value &lt; 10 )
{
// Do something if value is less than 10
}
else
{
// Do something else if value is greater than or equal to 10
}
</pre><p>If statements are used to decide whether to execute a part of the logic or not depending on a certain condition. The conditional expression must always evaluate to <code>true</code> or <code>false</code>.</p>
<p>It's possible to chain several <code>if-else</code> statements, in which case each condition will be evaluated sequencially until one is found to be <code>true</code>.</p>
<pre>
switch( value )
{
case 0:
// Do something if value equals 0, then leave
break;</pre><pre> case 2:
case constant_value:
// This will be executed if value equals 2 or the constant_value
break;</pre><pre> default:
// This will be executed if value doesn't equal any of the cases
}
</pre><p>If you have an integer (signed or unsigned) expression that have many different outcomes that should lead to different code, a switch case is often the best choice for implementing the condition. It is much faster than a series of ifs, especially if all of the case values are close in numbers.</p>
<p>Each case should be terminated with a break statement unless you want the code to continue with the next case.</p>
<p>The case value can be a constant variable that was initialized with a constant expression. If the constant variable was initialized with an expression that cannot be determined at compile time it cannot be used in the case values.</p>
<h1><a class="anchor" id="while"></a>
Loops: while / do-while / for</h1>
<pre>
// Loop, where the condition is checked before the logic is executed
int i = 0;
while( i &lt; 10 )
{
// Do something
i++;
}</pre><pre> // Loop, where the logic is executed before the condition is checked
int j = 0;
do
{
// Do something
j++;
} while( j &lt; 10 );
</pre><p>For both <code>while</code> and <code>do-while</code> the expression that determines if the loop should continue must evaluate to either true or false. If it evaluates to true, the loop continues, otherwise it stops and the code will continue with the next statement immediately following the loop.</p>
<pre>
// More compact loop, where condition is checked before the logic is executed
for( int n = 0; n &lt; 10; n++ )
{
// Do something
}
</pre><p>The <code>for</code> loop is a more compact form of a <code>while</code> loop. The first part of the statement (until the first <code>;</code>) is executed only once, before the loop starts. Here it is possible to declare a variable that will be visible only within the loop statement. The second part is the condition that must be satisfied for the loop to be executed. A blank expression here will always evaluate to true. The last part is executed after the logic within the loop, e.g. used to increment an iteration variable.</p>
<p>Multiple variables can be declared in the <code>for</code> loop, separated by <code>,</code>. Likewise, multiple increment expressions can be used in the last part by separating them with <code>,</code>.</p>
<h1><a class="anchor" id="break"></a>
Loop control: break / continue</h1>
<pre>
for(;;) // endless loop
{
// Do something</pre><pre> // End the loop when condition is true
if( condition )
break;
}
</pre><p><code>break</code> terminates the smallest enclosing loop statement or switch statement.</p>
<pre>
for(int n = 0; n &lt; 10; n++ )
{
if( n == 5 )
continue;</pre><pre> // Do something for all values from 0 to 9, except for the value 5
}
</pre><p><code>continue</code> jumps to the next iteration of the smallest enclosing loop statement.</p>
<h1><a class="anchor" id="return"></a>
Return statement</h1>
<pre>
float valueOfPI()
{
return 3.141592f; // return a value
}
</pre><p>Any function with a return type other than <code>void</code> must be finished with a <code>return</code> statement where expression evaluates to the same data type as the function return type. Functions declared as <code>void</code> can have <code>return</code> statements without any expression to terminate early.</p>
<h1><a class="anchor" id="block"></a>
Statement blocks</h1>
<pre>
{
int a;
float b;</pre><pre> {
float a; // Override the declaration of the outer variable
// but only within the scope of this block.</pre><pre> // variables from outer blocks are still visible
b = a;
}</pre><pre> // a now refers to the integer variable again
}
</pre><p>A statement block is a collection of statements. Each statement block has its own scope of visibility, so variables declared within a statement block are not visible outside the block.</p>
<h1><a class="anchor" id="try"></a>
Try-catch blocks</h1>
<pre>
{
try
{
DoSomethingThatMightThrowException();</pre><pre> // This is not executed if an exception was thrown
}
catch
{
// This is executed if an exception was thrown
}
}
</pre><p>A try-catch block can be used if you're executing some code that might throw an exception and you want to catch that exception and continue with the exception rather than just abort the script.</p>
<p>Exceptions can occur for various reasons, some examples include, accessing null pointers in uninitialized handles, division by zero, or exceptions raised from application registered functions. In some cases exceptions are intentionally raised by the script to interrupt some execution.</p>
<dl class="section see"><dt>See also</dt><dd><a class="el" href="doc_script_stdlib_exception.html">Exception handling</a> </dd></dl>
</div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="footer">Generated on Sat Dec 5 2020 23:20:24 for AngelScript by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.18 </li>
</ul>
</div>
</body>
</html>