Difference between revisions of "Statement Reference"

1,465 bytes added ,  01:11, 3 June 2019
m
no edit summary
imported>Jlundin
(Created page with "A statement is an arrangement of expressions used to perform work (and may just be a simple expression). There are also some more complicated statements ...")
 
imported>Teapare
m
 
(4 intermediate revisions by 4 users not shown)
Line 13: Line 13:
<br>
<br>
<source lang="papyrus">
<source lang="papyrus">
; Create a float variable name seconds that starts with a default value
; Create a float variable named seconds that starts with a default value
float seconds = CurrentTimeInMinutes() * 60.0f
float seconds = CurrentTimeInMinutes() * 60.0f
</source>
</source>
Line 33: Line 33:
; Assign 5 to x
; Assign 5 to x
x = 5
x = 5
;This assignment is equivalent to x = x + 5
x += 5
;This assignment is equivalent to x = x * (5 + 5)
x *= 5 + 5
</source>
</source>
<br>
<br>
Line 71: Line 77:
<source lang="papyrus">
<source lang="papyrus">
; If value is true, will set x to 1, otherwise it will do nothing
; If value is true, will set x to 1, otherwise it will do nothing
if value
if (value)
   x = 1
   x = 1
endIf
endIf
Line 78: Line 84:
<source lang="papyrus">
<source lang="papyrus">
; If myCoolValue is 20, will set x to 5, otherwise it will set it to 10
; If myCoolValue is 20, will set x to 5, otherwise it will set it to 10
if myCoolValue == 20
if (myCoolValue == 20)
   x = 5
   x = 5
else
else
Line 87: Line 93:
<source lang="papyrus">
<source lang="papyrus">
; If the value is above 10 it will set x to 1, if it is below 10 it will set x to -1, otherwise it will set x to 0
; If the value is above 10 it will set x to 1, if it is below 10 it will set x to -1, otherwise it will set x to 0
if value > 10
if (value > 10)
   x = 1
   x = 1
elseif value < 10
elseif (value < 10)
   x = -1
   x = -1
else
else
   x = 0
   x = 0
endIf
endIf
</source>
<br>
<source lang="papyrus">
; if the sum of x and y is less than 10, set z to 1, otherwise set z to -1.
if ((x + y) < 10)
    z = 1
else
    z = -1
endif
</source>
</source>


Line 107: Line 122:
; Loop until x is 10
; Loop until x is 10
x = 0
x = 0
while x < 10
while (x < 10)
   DoCoolStuff()
   DoCoolStuff()
   x += 1
   x += 1
endWhile
endWhile
</source>
=== While and Variable Lifetime ===
Variables defined inside While loops have a lifetime of as many times as the loop runs, not a lifetime of each iteration of the loop as one might expect. This means that it is very important to always assign a value to variables defined inside a While loop, or avoid defining variables inside While loops altogether.
Here is an example to illustrate this potential pitfall.
<source lang="papyrus">
int i = 0
while i < 5
    int j
    j += 1
    debug.trace("j = " + j)
endWhile
</source>
Here, j is defined inside the loop. Since we are calling "int j" each iteration, and since the default value of an integer is 0, we might expect output that resembles:
<source lang="papyrus">
[06/04/2016 - 01:24:38PM] j = 1
[06/04/2016 - 01:24:38PM] j = 1
[06/04/2016 - 01:24:38PM] j = 1
[06/04/2016 - 01:24:38PM] j = 1
[06/04/2016 - 01:24:38PM] j = 1
</source>
However, since j's value is maintained across iterations of the loop, we instead get:
<source lang="papyrus">
[06/04/2016 - 01:49:52PM] j = 1
[06/04/2016 - 01:49:52PM] j = 2
[06/04/2016 - 01:49:52PM] j = 3
[06/04/2016 - 01:49:52PM] j = 4
[06/04/2016 - 01:49:52PM] j = 5
</source>
</source>


Anonymous user