Statements

T

TheTacoBellD

Guest
<span class="hi">...</span>
<span class="keyword"> endif</span>
</pre>

<p>An if-then-else statement is a conditional branch that spans multiple lines.
In each condition block (begun with an <code>if</code> and ended with a
<code>endif</code>), each predicate expression is evaluated in turn (first the
one in the <code>if</code>, then the one in the next <code>elseif</code>,
etc.). Each must return a boolean value. The first predicate which evaluates
to true causes its associated statements to be executed (the statements in
the immediately proceeding sub-block). The remainder of the statements in
the other sub-blocks are ignored.

<p>If none of the predicates return true, then the <code>else</code> statements
are executed. There may be zero or more <code>elseif</code>s and the
<code>else</code> is optional (if it is ommitted, then nothing is executed
if no predicates are satisfied). For example:

<pre class="source">
<span class="comment">// (1)</span>
<span class="keyword"> if</span> <span class="hi">predicate</span> <span class="keyword">then</span>
<span class="hi">...</span>
<span class="keyword"> elseif</span> <span class="hi">predicate</span> <span class="keyword">then</span>
<span class="hi">...</span>
<span class="keyword"> endif</span>

<span class="comment">// (2)</span>
<span class="keyword"> if</span> <span class="hi">predicate</span> <span class="keyword">then</span>
<span class="hi">...</span>
<span class="keyword"> else</span>
<span class="hi">...</span>
<span class="keyword"> endif</span>

<span class="comment">// (3)</span>
<span class="keyword"> if</span> <span class="hi">predicate</span> <span class="keyword">then</span>
<span class="hi">...</span>
<span class="keyword"> endif</span>
</pre>

<p><strong>Loop</strong>

<pre class="source">
<span class="keyword"> loop</span>
<span class="hi">statement</span>
<span class="hi">statement</span>
<span class="hi">...</span>
<span class="keyword"> endloop</span>
</pre>

<p>A <code>loop</code> statement contains a block of statements that are
executed continuously. When execution reaches the end of the loop block,
we return to the top of the loop and begin again.

<p>To exit a loop block, one of the enclosed statements must be an
<code>exitwhen</code> statement. It is legal to have an infinite loop
(one that never exits; for example, a main-execution loop in an AI).

<p><strong>Exitwhen</strong>

<pre class="source">
<span class="keyword"> loop</span>
<span class="hi">...</span>
<span class="keyword"> exitwhen</span> <span class="hi">expression</span>
<span class="hi">...</span>
<span class="keyword"> endloop</span>
</pre>

<p>The <code>exitwhen</code> statement is used within a loop. The expression
must evaluate to a boolean value. If it evaluates to true, then execution
immediately exits the inner-most loop that the <code>exitwhen</code> is in.
The <code>exitwhen</code> may be within an if-then-else block inside the
loop and there may be multiple <code>exitwhen</code>s within a single loop;
for example:

<pre class="source">
<span class="keyword"> local</span> <span class="type">integer</span> iterations = 10
<span class="keyword"> loop</span>
<span class="keyword"> if</span> DidTheThingEnoughTimes() <span class="keyword">then</span>
<span class="keyword"> call</span> DisplayText(<span class="str">"Exiting Early"</span>)
<span class="keyword"> exitwhen</span> true
<span class="keyword"> endif</span>
<span class="keyword"> call</span> DoSomething()
<span class="keyword"> set</span> iterations = iterations - 1
<span class="keyword"> exitwhen</span> iterations == 0
<span class="keyword"> endloop</span>
</pre>

<p><strong>Return</strong>

<pre class="source">
<span class="keyword"> return</span> <span class="comment">// (1)</span>

<span class="keyword"> return</span> <span class="hi">expression</span> <span class="comment">// (2)</span>
</pre>

<p>The <code>return</code> statement causes execution to exit from the function
and return to the point after it was called. If the function is declared to
return a value, then the return statement must be used with an expression,
which is the value that is returned to the caller. The expression must evaluate
to a value that conforms to the function's declared type. There may be
multiple <code>return</code>s within a function. All execution paths within
a function must lead to a valid return (unless the function returns
<code>nothing</code>, in which case there is an implicit return when execution
reaches the end of the function).

<p><em>Important Note</em>: currently, the return value only needs to conform
to the native base type of the declared return type, not the declared return
type itself (E.G., if the declared type is <code>unit</code>, the value only
needs to conform to <code>handle</code>). This is a <strong>bug</strong> in
Blizzard's type checker. However, because this bug shows up in Blizzard's own
JASS code, the current version of the syntax checker purposely contains
this behavior also. You should avoid exploiting this because there is
almost no scenario where it is correct.

<p><strong>Debug</strong>

<pre class="source">
<span class="keyword"> debug</span> <span class="hi">statement</span>
</pre>

<p>Any <code>set</code>, <code>call</code>,
<code>if then else</code>, and <code>loop</code>
statement can be prefixed with <code>debug</code>. This
implies the statement is only executed if an game-internal debug flag is
enabled. You probably can't turn it on unless you work for Blizzard.



</body>
</html>


I might have possibly understood this passage from the JASS Manuel entitled "Statements", but the code makes it a lot more confusing believe it or not. So could anyone kind of sum up what "Statements" are?

I don't mean sum up this entire passage, I just want you to tell me a bit about them if possible. If I understand the subject some more I might understand this more clearly.

P.S. If there is something I should understand before learning about statements, please tell me.
 

SFilip

Gone but not forgotten
Reaction score
634
statements are similar to functions, but they work differently.
there are 2 types of statements in jass: if..then and loop.
i believe that if you understand these in gui (if conditions then actions and for each integer variable) you should have no trouble understanding them in jass.
if..then is defined like this
Code:
if <condition, should be boolean/boolexpr) then
    <actions>
else [COLOR=Red]//optional[/COLOR]
    <else actions>
endif
loops are created this way
Code:
loop
    <actions>
endloop
<actions> must contain exitwhen or else the loop will simply become infinite and crash.
exitwhen also needs a condition. best way to show this is a complete example...
Code:
local i = 0 [COLOR=Red]// first we need a variable to use[/COLOR]
loop
    exitwhen i == 10 [COLOR=Red]// every time the loop runs it will check if i is 10 and will break if it is[/COLOR]
    call BJDebugMsg(I2S(i)) [COLOR=Red]// simply show a text message that displays the current "position" of i[/COLOR]
    set i = i + 1 [COLOR=Red]// increase i by 1...or else you have an infine loop
endloop[/COLOR]
of course all of these lines can suit your needs. you can increase i by 2 or whatever you want. exitwhen can take any condition. for example you can make an action that kills all units and stops when the caster dies (exitwhen IsUnitDeadBJ(castervariable) == true). this is also another nice advantage of jass over gui ;)
 
T

TheTacoBellD

Guest
Cool. I'm pretty sure I get it. But should I first learn functions and THEN statements?
 

SFilip

Gone but not forgotten
Reaction score
634
not important...well you learned statements now, didn't you? there isn't really much more that i know of than the things mentioned in my post.
 
T

TheTacoBellD

Guest
SFilip said:
not important...well you learned statements now, didn't you? there isn't really much more that i know of than the things mentioned in my post.


Oh. Well that's good.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top