https://wiki.frictionalgames.com/page?title=HPL3/Scripting/AngelScript_Fundamentals/Chapter_5_-_Statements_and_Flow_Control&feed=atom&action=historyHPL3/Scripting/AngelScript Fundamentals/Chapter 5 - Statements and Flow Control - Revision history2024-03-29T12:48:59ZRevision history for this page on the wikiMediaWiki 1.34.2https://wiki.frictionalgames.com/page?title=HPL3/Scripting/AngelScript_Fundamentals/Chapter_5_-_Statements_and_Flow_Control&diff=4574&oldid=prevTiMan at 20:14, 15 August 20202020-08-15T20:14:03Z<p></p>
<table class="diff diff-contentalign-left" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #222; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #222; text-align: center;">Revision as of 20:14, 15 August 2020</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l441" >Line 441:</td>
<td colspan="2" class="diff-lineno">Line 441:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>{{note|If you remember, <code>break</code> is the same keyword that is used to mark the end of a <code>case</code> section in a <code>switch</code> statement. It's role there is the same as in a loop - it causes the program to exit the switch block instead of falling through to the next case section.}}</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>{{note|If you remember, <code>break</code> is the same keyword that is used to mark the end of a <code>case</code> section in a <code>switch</code> statement. It's role there is the same as in a loop - it causes the program to exit the switch block instead of falling through to the next case section.}}</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>{{NavBar|HPL3/Scripting/AngelScript Fundamentals/Chapter <del class="diffchange diffchange-inline">2 </del>- <del class="diffchange diffchange-inline">Structure of a Script</del>|Chapter <del class="diffchange diffchange-inline">2 </del>- <del class="diffchange diffchange-inline">Structure of a Script</del>|HPL3/Scripting/AngelScript Fundamentals|AngelScript Fundamentals|HPL3/Scripting/AngelScript Fundamentals/Chapter <del class="diffchange diffchange-inline">4 </del>- <del class="diffchange diffchange-inline">Operators and Expressions</del>|Chapter <del class="diffchange diffchange-inline">4 </del>- <del class="diffchange diffchange-inline">Operators and Expressions</del>}}</div></td><td class='diff-marker'>+</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>{{NavBar|HPL3/Scripting/AngelScript Fundamentals/Chapter <ins class="diffchange diffchange-inline">4 </ins>- <ins class="diffchange diffchange-inline">Operators and Expressions</ins>|Chapter <ins class="diffchange diffchange-inline">4 </ins>- <ins class="diffchange diffchange-inline">Operators and Expressions</ins>|HPL3/Scripting/AngelScript Fundamentals|AngelScript Fundamentals|HPL3/Scripting/AngelScript Fundamentals/Chapter <ins class="diffchange diffchange-inline">6 </ins>- <ins class="diffchange diffchange-inline">Functions</ins>|Chapter <ins class="diffchange diffchange-inline">6 </ins>- <ins class="diffchange diffchange-inline">Functions</ins>}}</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>[[Category:HPL3 Scripting]]</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>[[Category:HPL3 Scripting]]</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>[[Category:English]]</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>[[Category:English]]</div></td></tr>
</table>TiManhttps://wiki.frictionalgames.com/page?title=HPL3/Scripting/AngelScript_Fundamentals/Chapter_5_-_Statements_and_Flow_Control&diff=4572&oldid=prevTiMan: Created page with "{{shortPageTitle}} {{AngelScriptGuideMenu}} In the last lesson, you learned that expressions are lines of code where one or more values are manipulated or assigned to variabl..."2020-08-15T20:12:02Z<p>Created page with "{{shortPageTitle}} {{AngelScriptGuideMenu}} In the last lesson, you learned that expressions are lines of code where one or more values are manipulated or assigned to variabl..."</p>
<p><b>New page</b></p><div>{{shortPageTitle}}<br />
{{AngelScriptGuideMenu}}<br />
<br />
In the last lesson, you learned that expressions are lines of code where one or more values are manipulated or assigned to variables through the use of operators. There are other lines of code that do not use operators (or at least uses them in a different way). These lines of code are called ''statements''.<br />
<br />
The main use of statements in code is to control how a program moves from one line of code to the next. By default, when you run a program, it starts at the first line of code, moves down one line at a time, then stops once it passes the last line. This is well and good, but it's a bit limited, isn't it? What if you wanted to do part or all of your program multiple times? What if you wanted to skip part of it under certain conditions? These concepts are made possible in programming due to special statements that perform ''flow control''.<br />
<br />
== Conditions ==<br />
<br />
It is said that when the concept of conditionals enters programming, that is what truly separates a calculator from a computer. Being able to execute some code and skip other code based on a condition is incredibly powerful, and many programs wouldn't have been made possible without it.<br />
<br />
=== If ===<br />
<br />
The first step into conditional programming is the <code>if</code> keyword. When using <code>if</code>, you specify a condition in the form of a boolean value. If the value is true, the code under the <code>if</code> gets executed. If the value is false, then it doesn't.<br />
<br />
<syntaxhighlight lang="c++"><br />
int i = 0;<br />
<br />
if (true)<br />
{<br />
i = 1; // the boolean value is true, so this code executes<br />
}<br />
<br />
if (false)<br />
{<br />
i = 2; // the boolean value is false, so this code gets skipped<br />
}<br />
<br />
// at the end of this script, i is 1<br />
</syntaxhighlight><br />
<br />
The structure of an <code>if</code> statement is a bit different from what you've been used to up to this point, so lets break it down.<br />
<br />
First, you have the <code>if</code> keyword. Nothing groundbreaking quite yet. Next, you have a set of parentheses. These parentheses contain the condition that the <code>if</code> uses to decide whether or not to execute its code. (The parentheses are not optional, so don't forget to type them out.)<br />
<br />
Within the parentheses, you have the boolean condition. In this case, it just happens to be a literal boolean value (<code>true</code> in the first case, and <code>false</code> in the second).<br />
<br />
Then you have a set of curly braces that take up several lines of code. When this happens, the area inside the curly braces is called a ''code block''. Code blocks are used to say "this code belongs to a statement". In this case, this is the code block that belongs to the <code>if</code> statement - it is what may or may not execute depending on the condition.<br />
<br />
In this example, we used literal boolean values as the condition. This is pretty boring, though - the first <code>if</code> statement has <code>true</code> so it will always run and there's no point in having an <code>if</code> at all. The second <code>if</code> statement has <code>false</code>, so it will ''never'' run and there's no point in having any of the code inside that block. To make <code>if</code> statements a tad more useful, you can use variables as the condition as well:<br />
<br />
<syntaxhighlight lang="c++"><br />
bool b = true;<br />
<br />
if (b)<br />
{<br />
// Some code that runs if the value in <code>b</code> is true<br />
}<br />
</syntaxhighlight><br />
<br />
or even boolean expressions that utilize the comparison operators:<br />
<br />
<syntaxhighlight lang="c++"><br />
int a = 5;<br />
int b = 2;<br />
<br />
if (a > b)<br />
{<br />
// Some code that runs if the value in <code>a</code> is greater than the value in <code>b</code><br />
}<br />
</syntaxhighlight><br />
<br />
You can also chain together multiple conditions using the logic operators:<br />
<br />
<syntaxhighlight lang="c++"><br />
int a = 2;<br />
int b = 5;<br />
int c = 9;<br />
<br />
if (a < b && b < c)<br />
{<br />
// Some code that runs if the value in <code>a</code> is less than the value in <code>b</code> AND the value in <code>b</code> is less than the value in <code>c</code><br />
}<br />
</syntaxhighlight><br />
<br />
As you can see, you can get some pretty imaginative conditions going.<br />
<br />
=== Else ===<br />
<br />
In addition to <code>if</code>, there exists an <code>else</code> keyword. This keyword is a companion to <code>if</code>, and it will run a block of code if the condition within the <code>if</code> statement is false:<br />
<br />
<syntaxhighlight lang="c++"><br />
int i = 5;<br />
<br />
if (i == 2)<br />
{<br />
i = 3; // Doesn't run since <code>i</code> is not equal to 2<br />
}<br />
else<br />
{<br />
i = 4; // This runs since the condition was false<br />
}<br />
<br />
// At the end of this script, <code>i</code> is 4<br />
</syntaxhighlight><br />
<br />
As you can see, the code within the <code>else</code> statement's code block only executes if the condition within the <code>if</code> statement is false. The <code>if</code> statement and the <code>else</code> statement used together is informally referred to as an ''if-else'' statement.<br />
<br />
=== If-ElseIf-Else ===<br />
<br />
One trick you can do with <code>else</code> statements is that you can use them to chain together multiple if-else segments. This can be called an ''if-elseif-else'' statement:<br />
<br />
<syntaxhighlight lang="c++"><br />
int i = 5;<br />
<br />
if (i < 2)<br />
{<br />
i = 1; // Doesn't run since <code>i</code> is not less than 2<br />
}<br />
else if (i > 8)<br />
{<br />
i = 9; // Also doesn't run since <code>i</code> is not greater than 8<br />
}<br />
else <br />
{<br />
i = 6 // All other conditions are false, so this will run<br />
}<br />
<br />
// At the end of this script, <code>i</code> is 6<br />
</syntaxhighlight><br />
<br />
{{note|Mentally, the <code>if</code> statement works just like a logical conditional sentence in plain English (or your own language). In the previous <code>if</code> example, for instance, it can be read out like so:<br />
<br />
<syntaxhighlight lang="">If the value in <code>i</code> is equal to 2, set <code>i</code> to equal 3.</syntaxhighlight><br />
<br />
You can include the <code>else</code> statement in this sentence as well:<br />
<br />
<syntaxhighlight lang="">If the value in <code>i</code> is equal to 2, set <code>i</code> to equal 3, else set <code>i</code> equal to 2.</syntaxhighlight><br />
<br />
Forming your conditions in logical prose like this is a useful way to visualize how you want your conditions to be structured.}}<br />
<br />
=== Switch ===<br />
<br />
As you can see, you can get some neat little if-elseif-else statement chains going along. But what if you had something like this?<br />
<br />
<syntaxhighlight lang="c++"><br />
int i; // Is equal to some number<br />
<br />
if (i == 0)<br />
{<br />
// Do something if <code>i</code> is 0<br />
}<br />
else if (i == 1)<br />
{<br />
// Do something if <code>i</code> is 1<br />
}<br />
else if (i == 2)<br />
{<br />
// Do something if <code>i</code> is 2<br />
}<br />
else if (i == 3)<br />
{<br />
// Do something if <code>i</code> is 3<br />
}<br />
else if (i == 4)<br />
{<br />
// Do something if <code>i</code> is 4<br />
}<br />
// And so on...<br />
</syntaxhighlight><br />
<br />
That's a bit tedious, isn't it? Are you going to need to put in a new else-if statement for every single value that <code>i</code> could possibly be equal to?<br />
<br />
As it turns out, there is a better way - the <code>switch</code> statement:<br />
<br />
<syntaxhighlight lang="c++"><br />
int i; // Is equal to some number<br />
<br />
switch (i)<br />
{<br />
case 0:<br />
// Do something if <code>i</code> is equal to 0<br />
break;<br />
case 1:<br />
// Do something if <code>i</code> is equal to 1<br />
break;<br />
case 2:<br />
// Do something if <code>i</code> is equal to 2<br />
break;<br />
case 3:<br />
// Do something if <code>i</code> is equal to 3<br />
break;<br />
case 4:<br />
// Do something if <code>i</code> is equal to 4<br />
break;<br />
}<br />
</syntaxhighlight><br />
<br />
There's several bits of new information in there, so let's break it down:<br />
<br />
First, you have the initial <code>switch</code> statement. Like the <code>if</code> statement, you start with the <code>switch</code> keyword followed by a value inside parentheses, which is in turn followed by a code block. Unlike the <code>if</code> statement, the <code>switch</code> statement does not use a boolean condition. Instead, it takes an integer value directly, then executes one of several code possibilities based on what that value is.<br />
<br />
Within the <code>switch</code> statement's code block, there are several <code>case</code> keywords. <code>case</code> is used to start one of the "sub-block" sections that the <code>switch</code> statement could run. Each <code>case</code> keyword is followed by a value, denoting what value that case section represents. For example, if the value of <code>i</code> was 3, then the code within the <code>case 3</code> section would be run.<br />
<br />
At the end of each <code>case</code> section is a <code>break</code> keyword. This is another keyword related to flow control, and I will go into it in greater detail later on in this lesson. For now, just know that it marks the end of a <code>case</code> code section.<br />
<br />
There are a couple of other ways which you can use a <code>switch</code> statement. First, you can specify a <code>case</code> section that will run if the given value is one of several possibilities:<br />
<br />
<syntaxhighlight lang="c++"><br />
int i; // Is equal to some number<br />
<br />
switch (i)<br />
{<br />
case 0:<br />
case 1:<br />
case 2:<br />
// Do something if <code>i</code> is equal to 0, 1, or 2<br />
break;<br />
case 3:<br />
case 4:<br />
// Do something if <code>i</code> is equal to 3 or 4<br />
break;<br />
}<br />
</syntaxhighlight><br />
<br />
As you can see, there isn't any code placed inside cases 0, 1, or 3. This causes the case to "fall through" to the next case, making it so the code inside <code>case 2</code> will run if <code>i</code> is equal to 0 or 1 as well.<br />
<br />
You can also specify code to run if the given value doesn't match ''any'' of the cases:<br />
<br />
<syntaxhighlight lang="c++"><br />
int i; // Is equal to some number<br />
<br />
switch (i)<br />
{<br />
case 0:<br />
// Do something if <code>i</code> is equal to 0<br />
break;<br />
case 1:<br />
// Do something if <code>i</code> is equal to 1<br />
break;<br />
case 2:<br />
// Do something if <code>i</code> is equal to 2<br />
break;<br />
default:<br />
// Do something if <code>i</code> is something else entirely<br />
break;<br />
}<br />
</syntaxhighlight><br />
<br />
In this example, if <code>i</code> doesn't match any of the cases (say if <code>i</code> were equal to 3), then the code within the <code>default</code> section will be run instead.<br />
<br />
{{note|The <code>break</code> within the case sections is optional. If you do not put a <code>break</code> at the end of a case, then it will fall through just as though the that case section was empty. This is handy if you have code that you want to execute on one of several values, but other code that you only want to execute on a specific value:<br />
<br />
<syntaxhighlight lang="c++"><br />
int i;<br />
int a = 1;<br />
int b = 2;<br />
int c = 3;<br />
<br />
switch (i)<br />
{<br />
case 1:<br />
a = 4; // Runs only if <code>i</code> is equal to 0<br />
case 2:<br />
b = 5; // Runs if <code>i</code> is equal to 0 or 1<br />
case 3:<br />
c = 6; // Runs if <code>i</code> is equal to 0, 1, or 2<br />
break;<br />
}<br />
</syntaxhighlight>}}<br />
<br />
== Loops ==<br />
<br />
Conditionals are just one of the ways to have flow control in your program. Where conditions can make it so you can decide whether or not to execute some code, this section lets you specify whether to run some code ''over and over again for a specified amount of time''. Appropriately enough, these statements are called ''loops''.<br />
<br />
=== While ===<br />
<br />
The fundamental loop statement is called the ''while loop''. It's fairly simple in concept - just run a block of code for as long as a boolean condition is true:<br />
<br />
<syntaxhighlight lang="c++"><br />
int x = 0;<br />
<br />
while (x < 5)<br />
{<br />
x++;<br />
}<br />
</syntaxhighlight><br />
<br />
A <code>while</code> loop follows a specific pattern. When the program first reaches the <code>while</code> statement, it first checks the condition. If it is true, then the code within the code block is run. Once the code block finishes, the program goes ''back'' to the while statement and checks the condition again. If it's still true, then the code block runs a second time. This process repeats over and over again until the condition becomes false.<br />
<br />
In the above example, <code>x</code> is 0. 0 is less than 5, so the program enters the code block, which adds 1 to <code>x</code>. After that, it goes back to the <code>while</code> condition, which checks <code>x</code> again. <code>x</code> is now 1, which is still less than 5, so the code block runs and <code>x</code> gets 1 added to it again. This keeps going for when <code>x</code> is 2, 3, and 4. Once <code>x</code> becomes 5, then the condition is checked once more. <code>x</code> is now no longer less than 5 (as it is now equal to 5). The condition is false, so the program skips down below the code block and continues to run.<br />
<br />
{{note|One thing to be wary of when dealing with loops is the "endless loop". This happens when a condition for a loop is such that the condition will never be false. The quintessential example of this is the following:<br />
<br />
<syntaxhighlight lang="c++"><br />
while (true)<br />
{<br />
// Code in here will run forever<br />
}<br />
<br />
// Code out here will never run<br />
</syntaxhighlight><br />
<br />
Endless loops can never be broken, so the only way to get out of them is to force-close your program. In the programmer world, this is generally considered to be bad design.}}<br />
<br />
=== Do-While ===<br />
<br />
A do-while loop is very similar to a while loop with one key difference. Take a look at the following example and see if you can tell what it is:<br />
<br />
<syntaxhighlight lang="c++"><br />
int x = 0<br />
<br />
do<br />
{<br />
x++;<br />
} while (x < 5);<br />
</syntaxhighlight><br />
<br />
The <code>do</code> takes the place of the <code>while</code> from a while loop, and the <code>while</code> keyword and condition is moved to after the code block. This reflects the single difference between a while loop and a do-while loop - a do-while loop will always run its code block at least once. More specifically, where the while loop checks its condition ''before'' it runs its code block, the do-while loop checks its condition ''after'' running its code block each time.<br />
<br />
To demonstrate this, look at the following:<br />
<br />
<syntaxhighlight lang="c++"><br />
// While loop<br />
int x = 0;<br />
while (false)<br />
{<br />
x = 1;<br />
}<br />
<br />
// Do-While Loop<br />
do<br />
{<br />
x = 1;<br />
} while (false);<br />
</syntaxhighlight><br />
<br />
The condition inside the while statement for both loops is set to <code>false</code>, so it is guaranteed to fail the condition check. For the while loop, this means that the code inside the code block never runs, and <code>x</code> will remain at 0. However, since a do-while loop is guaranteed to run its code block at least once, <code>x</code> will be 1 once the loop is terminated.<br />
<br />
=== For ===<br />
<br />
The while and do-while loop are great for when you want to repeat an action multiple times, but what if you want to operate over a range of values? Remember back in Lesson 2 and we were talking about arrays? Initializing an array was easy - all you had to do was declare an array variable. But what if you wanted to fill it full of values? Before, you would do it like this:<br />
<br />
<syntaxhighlight lang="c++"><br />
array<int> arr;<br />
arr[0] = 0;<br />
arr[1] = 1;<br />
arr[2] = 2;<br />
arr[3] = 3;<br />
arr[4] = 4;<br />
</syntaxhighlight><br />
<br />
That's a bit wordy, isn't it? Although, all those lines of code do look repetitive, so this is a case where we can use loops to make it easier:<br />
<br />
<syntaxhighlight lang="c++"><br />
array<int> arr;<br />
int i = 0;<br />
<br />
while (i < 5)<br />
{<br />
arr[i] = i;<br />
i++;<br />
}<br />
</syntaxhighlight><br />
<br />
That's a bit fancier. Instead of explicitly writing out each number, we use a loop that makes <code>i</code> go through each of the numbers with a loop and assign that number to each index of the array. It's still a bit wordy, though.<br />
<br />
As it turns out, though, there's a loop statement designed specifically for this kind of situation: the ''for loop'':<br />
<br />
<syntaxhighlight lang="c++"><br />
array<int> arr;<br />
<br />
for (int i = 0; i < 5; i++)<br />
{<br />
arr[i] = i;<br />
}<br />
</syntaxhighlight><br />
<br />
Now that's concise. Let's go over how it all comes together.<br />
<br />
First, as usual, the <code>for</code> keyword comes first, followed by some code surrounded by parentheses, and finally a code block. This code is split into three sections - a ''setup'', a ''condition'', and an ''increase''. The setup code is run once, before the loop starts for the first time. Then the condition code is checked similar to the while loop - if the condition is true, then the code block is run. After the code block finishes, the code in the increase is run, followed by the condition getting checked again. This process is repeated (code block, increase, condition) until the condition becomes false.<br />
<br />
In the above example, it starts by declaring the <code>i</code> variable and initializing it to 0. Then the condition is checked, which is true since 0 is less than 5. The code block runs, after which the increase section adds 1 to <code>i</code>. <code>i</code> is now 1, which is still less than 5, so the code block runs again. <code>i</code> becomes 2, 3, and 4, but once it becomes 5, the condition becomes false and the loop terminates.<br />
<br />
{{note|All of the loop statements have been accompanied by a code block surrounded by curly braces, but this isn't actually strictly necessary. Instead, you can have a single line of code following the loop statement, and this line of code becomes the "code block" of the loop. For example, if you have the following loop:<br />
<br />
<syntaxhighlight lang="c++"><br />
while (some condition)<br />
{<br />
// some code<br />
}<br />
</syntaxhighlight><br />
<br />
The brackets can be removed entirely, leaving the following:<br />
<br />
<syntaxhighlight lang="c++"><br />
while (some condition)<br />
// some code<br />
</syntaxhighlight><br />
<br />
You can do this with the do-while loop and the for loop as well. Keep in mind, though, that this only works if the code body consists of a single line. If you have two lines:<br />
<br />
<syntaxhighlight lang="c++"><br />
while (some condition)<br />
// some code<br />
// some other code<br />
</syntaxhighlight><br />
<br />
The second line of code is not recognized as being part of the loop, so it won't run until the loop terminates.}}<br />
<br />
=== Loop Control ===<br />
<br />
When you set up a loop, it will run until its condition becomes false (even if it has to run forever). However, sometimes, you want to have a bit more control over how the loop runs. For these cases, there are two special keywords - <code>continue</code> and <code>break</code>.<br />
<br />
When you use <code>continue</code> in a loop, it tells the loop to skip the rest of the code block and jump straight to checking the condition (or, in the case of a for loop, running the ''increase'' code):<br />
<br />
<syntaxhighlight lang="c++"><br />
int x = 0;<br />
<br />
while (x < 5)<br />
{<br />
x++;<br />
cLux_AddDebugMessage("A");<br />
<br />
continue;<br />
<br />
cLux_AddDebugMessage("B");<br />
}<br />
</syntaxhighlight><br />
<br />
If you had this code running in HPL3, it would print "A" 5 times but "B" only once. That's because the <code>continue</code> causes the "B" line of code to not run as the program skips the rest of the code block and goes right back to the condition.<br />
<br />
The second loop control keyword is <code>break</code>. What it does is it causes the loop to stop entirely. It skips the rest of the code block and terminates the loop:<br />
<br />
<syntaxhighlight lang="c++"><br />
int x = 0;<br />
<br />
while (x < 5)<br />
{<br />
x++;<br />
cLux_AddDebugMessage("A");<br />
<br />
break;<br />
<br />
cLux_AddDebugMessage("B");<br />
}<br />
</syntaxhighlight><br />
<br />
Run in HPL3, this code would only print a single "A", even though its logic would say that it should loop 5 times. That's because when the program reaches the <code>break</code>, the loop stops right where it is and exits, going to the next code after the loop.<br />
<br />
{{note|If you remember, <code>break</code> is the same keyword that is used to mark the end of a <code>case</code> section in a <code>switch</code> statement. It's role there is the same as in a loop - it causes the program to exit the switch block instead of falling through to the next case section.}}<br />
<br />
{{NavBar|HPL3/Scripting/AngelScript Fundamentals/Chapter 2 - Structure of a Script|Chapter 2 - Structure of a Script|HPL3/Scripting/AngelScript Fundamentals|AngelScript Fundamentals|HPL3/Scripting/AngelScript Fundamentals/Chapter 4 - Operators and Expressions|Chapter 4 - Operators and Expressions}}<br />
<br />
[[Category:HPL3 Scripting]]<br />
[[Category:English]]</div>TiMan