Ifvar conditions: Difference between revisions

From EDukeWiki
Jump to navigation Jump to search
mNo edit summary
No edit summary
 
(9 intermediate revisions by 3 users not shown)
Line 1: Line 1:
'''ifvar'''<'''conditional'''> <[[gamevar]]> <value><br>
<span {{code}}>'''if'''<'''conditional'''> <gamevar> <constant|gamevar> <then_branch> ['''[[else]]''' <else_branch>]</span>
'''ifvarvar'''<'''conditional'''> <[[gamevar]]> <value>


Ifvar conditions are special [[if condition]]s that apply to [[gamevar]]s. These statements work by comparing the two inputs using the conditional provided as part of the command. If the resulting comparison or operation is true, the game will then execute the code specified. If only one CON command is to be performed, simply put it after the condition. For best reading, put it on the same line or on the following line, indented.  For longer chains, enclose the code in curly braces ("{" and "}"). For performance reasons, do not use curly braces for single commands.  [[Else]] statements can also be used. If you want to do nothing, followed by an else, in place of a command you can write <code>[[nullop]]</code> or <code>{ }</code>.
<span {{code}}>'''ifvar'''<'''conditional'''> <gamevar> <constant> <then_branch> ['''[[else]]''' <else_branch>]</span> ''(deprecated)''
 
<span {{code}}>'''ifvarvar'''<'''conditional'''> <gamevar> <gamevar> <then_branch> ['''[[else]]''' <else_branch>]</span> ''(deprecated)''
 
Ifvar conditions are [[if condition]]s that apply to [[gamevar]]s.
 
Gamevar conditionals in CON can take only 2 arguments at a time, the first always being the variable being compared against the second operand. The second operand can be a constant, a [[define|label]] or another [[gamevar|variable]].
 
If the comparison returns true, the game will execute the code in the ''<then_branch>''. If false, the ''<else_branch>'' may be executed if present, otherwise the block is skipped.
 
The branches may consist of a single command, or if multiple commands need to be executed, a block of code enclosed in curly braces (<code>{</code> and <code>}</code>). For performance reasons, do not use curly braces for single commands.   
 
To negate a conditional (e.g. you want to check that an '''and''' is ''not'' true) place a [[nullop]] inside the ''<then>'' branch, and put the code to be executed inside the ''<else>'' branch.
 
See also: [[else]]
 
== Chaining if statements ==
 
To check for more than a single condition, the '''if''' statements will need to be chained.
 
If done without curly braces, this acts in much the same way as <code>&&</code> does in C/C++ syntax. Namely, an [[else]] branch that follows the '''if''' chain will be executed if any of the preceeding '''if''' conditionals returns false.
 
====Example====
definequote 100 Hello World
set temp 0
ife temp 1
  ife temp 0
{
    nullop
}
else
{
    // will be executed if either of the above conditionals returns false
    quote 100
}
 
This may be counterintuitive, as C/C++ and other programming languages only associate the '''[[else]]''' statement to a single '''if''', usually the one in the same context, which is not the case in CON.
 
== Types of ifvar conditions ==


{| {{prettytable}}
{| {{prettytable}}
Line 8: Line 47:
|-
|-
|e
|e
|==
|<code>==</code>
|=
|=
|equal to
|equal to
|-
|n
|<code>!=</code>
|&ne;
|not equal to
|-
|g
|<code>></code>
|>
|greater than
|-
|ge
|<code>>=</code>
|&ge;
|greater or equal
|-
|a
|
|
|above (unsigned greater than)
|-
|ae
|
|
|above or equal (unsigned greater or equal)
|-
|-
|l
|l
|<
|<code><</code>
|<
|<
|less than
|less than
|-
|-
|g
|le
|>
|<code><=</code>
|>
|&le;
|greater than
|less or equal
|-
|b
|
|
|below (unsigned less than)
|-
|-
|n
|be
|!=
|
|&ne;
|
|not equal to
|below or equal (unsigned less or equal)
|-
|-
|and
|and
|&
|<code>&</code>
|  
|  
|bitwise AND
|bitwise AND
|-
|-
|or
|or
|<nowiki>|</nowiki>
|<code><nowiki>|</nowiki></code>
|  
|  
|bitwise OR
|bitwise OR
|-
|-
|xor
|xor
|^
|<code>^</code>
|  
|  
|bitwise XOR
|bitwise XOR
|-
|both
|<code><nowiki>&&</nowiki></code>
|
|logical AND: both are non-zero
|-
|-
|either
|either
|<nowiki>||</nowiki>
|<code><nowiki>||</nowiki></code>
|
|
|logical OR: either are non-zero
|logical OR: either are non-zero
|}
|}


Since CON variables are integer-only, "less-than or equal to" (le, <=, &le;) and "greater-than or equal to" (ge, >=, &ge;) conditionals are unnecessary.
The above and below operators are useful for validating that a gamevar is a valid index into an array, because by interpreting its value as [[Wikipedia:signedness|unsigned]] when comparing against the array size, you can rule out when the gamevar is negative and when it is larger than or equal to the array size in one comparison operation instead of two.


Commands with an additional "var" suffix take [[gamevar]]s rather than constants or [[define]]d labels for their inputs.
{{varsuffix}}


[[Category:Gamevar manipulation]]
[[Category:Gamevar manipulation]]
[[Category:If conditions]]

Latest revision as of 11:05, 15 December 2024

if<conditional> <gamevar> <constant|gamevar> <then_branch> [else <else_branch>]

ifvar<conditional> <gamevar> <constant> <then_branch> [else <else_branch>] (deprecated)

ifvarvar<conditional> <gamevar> <gamevar> <then_branch> [else <else_branch>] (deprecated)

Ifvar conditions are if conditions that apply to gamevars.

Gamevar conditionals in CON can take only 2 arguments at a time, the first always being the variable being compared against the second operand. The second operand can be a constant, a label or another variable.

If the comparison returns true, the game will execute the code in the <then_branch>. If false, the <else_branch> may be executed if present, otherwise the block is skipped.

The branches may consist of a single command, or if multiple commands need to be executed, a block of code enclosed in curly braces ({ and }). For performance reasons, do not use curly braces for single commands.

To negate a conditional (e.g. you want to check that an and is not true) place a nullop inside the <then> branch, and put the code to be executed inside the <else> branch.

See also: else

Chaining if statements

To check for more than a single condition, the if statements will need to be chained.

If done without curly braces, this acts in much the same way as && does in C/C++ syntax. Namely, an else branch that follows the if chain will be executed if any of the preceeding if conditionals returns false.

Example

definequote 100 Hello World
set temp 0

ife temp 1
  ife temp 0
{
    nullop
}
else
{
    // will be executed if either of the above conditionals returns false
    quote 100
}

This may be counterintuitive, as C/C++ and other programming languages only associate the else statement to a single if, usually the one in the same context, which is not the case in CON.

Types of ifvar conditions

Conditional C/C++ Math Description
e == = equal to
n != not equal to
g > > greater than
ge >= greater or equal
a above (unsigned greater than)
ae above or equal (unsigned greater or equal)
l < < less than
le <= less or equal
b below (unsigned less than)
be below or equal (unsigned less or equal)
and & bitwise AND
or | bitwise OR
xor ^ bitwise XOR
both && logical AND: both are non-zero
either || logical OR: either are non-zero

The above and below operators are useful for validating that a gamevar is a valid index into an array, because by interpreting its value as unsigned when comparing against the array size, you can rule out when the gamevar is negative and when it is larger than or equal to the array size in one comparison operation instead of two.

Commands with an additional "var" suffix take gamevars rather than constants or defined labels for their inputs. As an alternate short form, "varvar" can be dropped from these commands; for example ife serves an an alias for ifvarvare.