If

From Wiki
Revision as of 04:13, 22 July 2008 by Orngrimm (Talk | contribs)

Jump to: navigation, search

Synopsis

if ( ! ) ( expression ) { }

or

if ( ! ) ( expression ) { } else { }

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 can group several statements into a statement group. 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

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