Control Structures

From Wiki
Revision as of 13:44, 18 August 2005 by 192.88.212.56 (Talk)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

if

The if construct is one of the most important feature of many languages, the EasyUO scripting language included. It allows for conditional execution of code fragments. The basic syntax of the command is:

if ( expression )
	statement

As described in the section about expressions expr is evaluated to its boolean value. If it evaluates to #true the statement will be executed.

The following code fragment will display 'a is bigger than b' if %a is bigger than %b:

initEvents

set %a 1
set %b 0
if %a > %b
	event SysMessage a is bigger than b

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, this code would display a is bigger than b if %a is bigger than %b, and would then assign the value of %a into %b:

initEvents

set %a 10
set %b 2
if %a > %b
{
	event SysMessage a is bigger than b
	set %b %a
}

if statements can be nested indefinitely within other if statements, which provides you with complete flexibility for conditional execution of the various parts of your script.

else

Often you would want to execute a statement if a certain condition is met, and a different statement if the condition is not met. This is what else is for. else extends an if statement to execute a statement in case the expression in the if statement evaluates to #false. For example, the following code would display a is bigger than b if %a is bigger than %b, and a is NOT bigger than b otherwise:

initEvents

set %a 10
set %b 20
if %a > %b
{
  event SysMessage a is bigger than b
}
else
{
  event SysMessage a is NOT bigger than b 
'''Note''' 
Right now it is not possible to nest if commands and use else. Only one if, if you are using else.