Difference between revisions of "If"
(Added info on specifying how many lines are to be evaluted.) |
|||
(7 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
{{command header|Flow Control}} | {{command header|Flow Control}} | ||
− | {{body|if ( expression ) { } | + | {{body|if ( ! ) ( expression ) ( lines <nowiki>|</nowiki> { } ) |
− | + | :''or'' | |
− | The following code fragment will display < | + | if ( ! ) ( expression ) ( lines <nowiki>|</nowiki> { } ) else ( lines <nowiki>|</nowiki> { } ) |The <tt>if</tt> construct is one of the most important features of many languages, the EasyUO scripting language included. It allows for conditional execution of code fragments. |
+ | |||
+ | As described in the section about [[expressions]], <tt>expression</tt> is evaluated to its boolean value. If it evaluates to [[true]], the next statement will be executed. If <tt>else</tt> is present and the expression evaluates to [[false]], then the statement after <tt>else</tt> will be executed instead. | ||
+ | |||
+ | If <tt>%a > %b</tt> evaluates to [[true]] in the following code fragment, it will display <i>"a is bigger than b"</i>; if it evaluates to [[false]], <i>"a is equal or less than b"</i> will be displayed instead: | ||
<pre> | <pre> | ||
− | set %a | + | set %a 100 |
− | set %b | + | set %b #random |
if %a > %b | if %a > %b | ||
display ok a is bigger than b | display ok a is bigger than b | ||
+ | else | ||
+ | display ok a is equal or less than b | ||
+ | halt | ||
</pre> | </pre> | ||
− | Often you would want to have more than one statement executed conditionally. Of course, there is no need to wrap each statement in an if clause. Instead, you can group several statements into a statement group. For example, | + | Often you would want to have more than one statement executed conditionally. Of course, there is no need to wrap each statement in an if clause. Instead, you have two options. You can group several statements into a statement group or you can specify how many lines after the if statement are to be evaluated. For example, if <tt>%a > %b</tt> evaluates to [[true]] in the following fragment, it would display <i>"a is bigger than b"</i> and would then assign the value of <tt>%a</tt> into <tt>%b</tt>; if it evaluates to [[false]], <i>"b is equal or bigger than a"</i> would be displayed and the value of <tt>%b</tt> assigned into <tt>%a</tt> instead: |
<pre> | <pre> | ||
− | set %a | + | set %a 100 |
− | set %b | + | set %b #random |
if %a > %b | if %a > %b | ||
{ | { | ||
Line 20: | Line 27: | ||
set %b %a | set %b %a | ||
} | } | ||
+ | else | ||
+ | { | ||
+ | display ok b is equal or bigger than a | ||
+ | set %a %b | ||
+ | } | ||
+ | halt | ||
</pre> | </pre> | ||
− | |||
− | + | OR | |
+ | |||
<pre> | <pre> | ||
− | + | set %a 100 | |
+ | set %b #random | ||
+ | if %a > %b 2 | ||
+ | |||
+ | display ok a is bigger than b | ||
+ | set %b %a | ||
+ | else 2 | ||
+ | display ok b is equal or bigger than a | ||
+ | set %a %b | ||
+ | halt | ||
+ | </pre> | ||
+ | |||
+ | |||
+ | The ! right after the command is a reversal of the expression which is following. | ||
+ | <pre> | ||
+ | set %var hello | ||
+ | if %var = world | ||
+ | display ok This shouldnt be displayed. | ||
+ | if %var <> world | ||
+ | display ok This should be displayed because hello is another (<>) than world. | ||
+ | if ! %var = world | ||
+ | display ok this should be displayed because hello is not equal world. | ||
+ | stop</pre>}} | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ==== Nesting Conditional Statements ==== | ||
+ | <tt>if</tt> statements can be nested indefinitely within other <tt>if</tt> or <tt>else</tt> statements, which provides you with complete flexibility for conditional execution of the various parts of your script. | ||
+ | <pre> | ||
+ | set %a #random % 3 + 1 | ||
+ | if %a = 1 | ||
+ | { | ||
+ | display ok One | ||
+ | } | ||
+ | else | ||
{ | { | ||
− | + | if %a = 2 | |
− | + | { | |
+ | display ok Two | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | display ok Three | ||
+ | } | ||
} | } | ||
+ | halt | ||
</pre> | </pre> | ||
− | {{ | + | == Related Commands == |
+ | {| style="background:gainsboro; color:black; border: 2px #aaa solid;" | ||
+ | | Width=250px align=center | [[Else]] | ||
+ | |} | ||
+ | =See Also= | ||
+ | {{Flow Control}} |
Latest revision as of 09:46, 30 November 2008
Synopsis
if ( ! ) ( expression ) ( lines | { } )
- or
if ( ! ) ( expression ) ( lines | { } ) else ( lines | { } )
Description
The if construct is one of the most important features of many languages, the EasyUO scripting language included. It allows for conditional execution of code fragments.
As described in the section about expressions, expression is evaluated to its boolean value. If it evaluates to true, the next statement will be executed. If else is present and the expression evaluates to false, then the statement after else will be executed instead.
If %a > %b evaluates to true in the following code fragment, it will display "a is bigger than b"; if it evaluates to false, "a is equal or less than b" will be displayed instead:
set %a 100 set %b #random if %a > %b display ok a is bigger than b else display ok a is equal or less than b halt
Often you would want to have more than one statement executed conditionally. Of course, there is no need to wrap each statement in an if clause. Instead, you have two options. You can group several statements into a statement group or you can specify how many lines after the if statement are to be evaluated. For example, if %a > %b evaluates to true in the following fragment, it would display "a is bigger than b" and would then assign the value of %a into %b; if it evaluates to false, "b is equal or bigger than a" would be displayed and the value of %b assigned into %a instead:
set %a 100 set %b #random if %a > %b { display ok a is bigger than b set %b %a } else { display ok b is equal or bigger than a set %a %b } halt
OR
set %a 100 set %b #random if %a > %b 2 display ok a is bigger than b set %b %a else 2 display ok b is equal or bigger than a set %a %b halt
The ! right after the command is a reversal of the expression which is following.
set %var hello if %var = world display ok This shouldnt be displayed. if %var <> world display ok This should be displayed because hello is another (<>) than world. if ! %var = world display ok this should be displayed because hello is not equal world. stop
Nesting Conditional Statements
if statements can be nested indefinitely within other if or else statements, which provides you with complete flexibility for conditional execution of the various parts of your script.
set %a #random % 3 + 1 if %a = 1 { display ok One } else { if %a = 2 { display ok Two } else { display ok Three } } halt
Related Commands
Else |
See Also
Flow Control
- Flow control commands allow scripts to make decisions based on the evaluation of boolean expressions.
break | Jumps to first statement outside of loop |
call | Transfers execution to another script file |
continue | Jumps execution of a loop to next iteration |
exit | Exits a called script |
for | Creates a counting loop |
gosub | Transfers execution to the matching sub |
goto | Jumps to another part of the script given by a label |
halt | Stops the script |
if | Executes code based on the evaluation of an expression |
pause | Temporarily stops the execution of the current script |
repeat..until | Creates a loop that checks condition after execution |
return | Returns from a sub |
stop | Ends the script |
while | Creates a loop that checks condition before execution |