Difference between revisions of "Statement Reference"

From the CreationKit Wiki
Jump to navigation Jump to search
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>Ditre
(not having the conditional values in parenthesis caused undesirable results while testing my own codes)
Line 71: Line 71:
<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 78:
<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 87:
<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
Line 107: Line 107:
; 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

Revision as of 14:33, 5 March 2012

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 like "if" and "while".

Define Statement

<define statement> ::= <type> <identifier> ['=' <expression>]

A define statement defines a single variable and, optionally, initializes it to a value. If a value is not given to it, it starts with the standard default value. A variable defined inside a function does not conflict with a variable defined in another function. A variable defined in an if or while block will not conflict with a variable defined in another if or while block that is not a child or a parent of the block that defined it.

Examples

; Create an integer variable named var that starts with the default value of 0
int var


; Create a float variable name seconds that starts with a default value
float seconds = CurrentTimeInMinutes() * 60.0f

Assign Statement

<assign statement> ::= (<l-value> '=' <expression>) |
                       (<l-value> '+=' <expression>) |
                       (<l-value> '-=' <expression>) |
                       (<l-value> '*=' <expression>) |
                       (<l-value> '/=' <expression>) |
                       (<l-value> '%=' <expression>)
<l-value>          ::= ([<expression> '.'] <identifier>)
                       (<expression> '[' <expression> ']')

An assignment statement calculates the results of the expression to the left of the assignment operator, and variable referred to by the l-value, and either assigns the result to the l-value, or modifies the l-value by the result.

Examples

; Assign 5 to x
x = 5


; Increment the property by the calculated value
MyObject.MyProperty += CoolFunction() * 10

Return Statement

'Return' [<expression>]

The return statement immediately stops running the function, calculates the result of the expression (if it exists), and returns it to the caller. The type the expression resolves to must match the return type of the function. If the function has no return type, then just use a return statement with no expression. If a function with a return type exits without a return statement, then None will be return (and a warning printed by the game if None is not allowed to be assigned to the return type)

Examples

; Assuming the return type is int, return the value to the caller
Return 5


; Return immediately, returning nothing
Return
x = 5 ; This never runs, because execution has exited this function

If Statement

<if statement> ::= 'if' <expression>
                     <statement>*
                   ['elseif' <expression>
                     <statement>*]*
                   ['else'
                     <statement>*]
                   'endIf'

The if statement calculates its expression and, if the result is true, runs the statements underneath it until it runs into an "elseif", "else", or "endif". If the expression's result is false, then it jumps down the if, checking each "elseif" as well until it hits an "else" (which then runs its statements) or an "endif". At each "elseif" encountered, it will check the expression and, if true, run the statements under it and if false, will jump to the next "elseif", "else", or "endIf".

Examples

; If value is true, will set x to 1, otherwise it will do nothing
if (value)
  x = 1
endIf


; If myCoolValue is 20, will set x to 5, otherwise it will set it to 10
if (myCoolValue == 20)
  x = 5
else
  x = 10
endIf


; 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)
  x = 1
elseif (value < 10)
  x = -1
else
  x = 0
endIf

While Statement

'while' <expression>
  <statement>*
'endWhile'

The while statement is a loop and will repeatedly execute the statements inside it until the expression is false.

Examples

; Loop until x is 10
x = 0
while (x < 10)
  DoCoolStuff()
  x += 1
endWhile