Difference between revisions of "Control Structures"

From Wiki
Jump to: navigation, search
(content, formatting, etc.)
Line 1: Line 1:
'''if'''
+
==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:
+
The “if” construct, one of the most important features of many languages including EasyUO, allows for conditional execution of code fragments. The basic syntax of the command is:
  
<pre>
+
If ( expression )
if ( expression )
+
statement</pre>
+
  
As described in the section about expressions expr is evaluated to its boolean value. If it evaluates to #true the statement will be executed.
+
{ statement }
  
The following code fragment will display 'a is bigger than b' if %a is bigger than %b:
+
As described in the section about [[Expressions]], expression is evaluated as a Boolean value. If it evaluates to [[var_true|#true]] the statement will be executed.
 +
 
 +
The following code fragment will display a message box with the phrase "a is greater than b" ''if'' the value in the variable %a is greater than the value in the variable %b:
  
 
<pre>
 
<pre>
initEvents
 
 
 
set %a 1
 
set %a 1
 
set %b 0
 
set %b 0
 
if %a > %b
 
if %a > %b
event SysMessage a is bigger than b</pre>
+
display ok a is greater than b
 +
halt</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, 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:
+
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 code block by using braces. For example, this code would display a message box with the phrase "a is greater than b" ''if'' the vaule in the variable %a is greater than the value in the variable %b, and would then assign the value of %a into the variable %b:
  
 
<pre>
 
<pre>
initEvents
 
 
 
set %a 10
 
set %a 10
 
set %b 2
 
set %b 2
 
if %a > %b
 
if %a > %b
 
{
 
{
event SysMessage a is bigger than b
+
display ok a is greater than b
 
set %b %a
 
set %b %a
}</pre>
+
}
 +
halt</pre>
  
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.
+
“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'''
+
==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:  
+
Often you would want to execute a statement or code block if a certain condition is met, and a different statement or code block if the condition is not met. This is the purpose of the else statement. Else extends an “if” statement to execute a statement or code block only in the instance that the expression of the previous if statement evaluates to [[var_false|#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:  
  
 
<pre>
 
<pre>
initEvents
 
 
 
set %a 10
 
set %a 10
 
set %b 20
 
set %b 20
 
if %a > %b
 
if %a > %b
 
{
 
{
  event SysMessage a is bigger than b
+
display ok a is greater than b
 
}
 
}
 
else
 
else
 
{
 
{
  event SysMessage a is NOT bigger than b </pre>
+
display ok a is less than b
 +
}
 +
halt</pre>
  
<pre>
+
''Note: in EasyUO 1.4 it is not possible to nest if commands and use else. The method around this limitation is to use an additional if to test for the opposite condition. In EasyUO 1.5, if and else can be safely nested however.''
Note:
+
Right now it is not possible to nest if commands and use else. Only one if, if you are using else.</pre>
+
  
'''for'''
+
==for==
The for loop is a loop that counts a variable either up or down.
+
The “for” loop is a loop that iterates a variable either up or down upon execution of the code block that it controls. The code block is executed with each iteration until the second value is reached.
  
 
<pre>
 
<pre>
Line 64: Line 60:
 
}</pre>
 
}</pre>
  
The loop starts by assigning the from_value to the variable, runs the loop. Then it either increments or decrements, depening on which way the loop is going, running the loop for each value.
+
The loop starts by assigning the ''from_value'' to the variable, runs the statement(s) inside the code block. The variable is then incremented or decremented as required for the variable to be one integer value closer to the ''to_value''. The statement inside the code block is executed after each and every iteration of the variable.
  
<pre>
+
''Note: in EasyUO 1.4, brackets are required to be used with a for statement, even if the code block consists only of a single statement. As of EasyUO 1.5, if the code to be executed with a for statement is only a single statement, the braces are optional.''
Note
+
At the moment, you must ALWAYS use brackets with FOR. </pre>
+
  
'''Subs'''
+
==Subs==
 
The term sub is short for subroutine. Subs are an important feature of the EasyUO script, that will make your scripts more logically structured, easier to read, and will make you have to write less code.
 
The term sub is short for subroutine. Subs are an important feature of the EasyUO script, that will make your scripts more logically structured, easier to read, and will make you have to write less code.
  
Line 82: Line 76:
 
If parameters were added to the calling gosub command they will be present in the variables %1, %2, and so on. The variable %0 holds the number of parameters passed. As all variables are in the global scope, the %0, %1, %2.. will be overwritten if you call another sub from inside a sub.
 
If parameters were added to the calling gosub command they will be present in the variables %1, %2, and so on. The variable %0 holds the number of parameters passed. As all variables are in the global scope, the %0, %1, %2.. will be overwritten if you call another sub from inside a sub.
  
<pre>
+
''Warning: Do not GOTO out of a sub, always use return! EasyUO will probably not crash but it is '''very''' bad programming style.''
Note
+
Don't GOTO out of a sub. Always use return! EasyUO will probably not crash but it is VERY bad programming style. </pre>
+
  
Currently, inline comments don't work with the sub command (this'll get fixed in a future update).  
+
''Note: inline comments do not work with the sub command in EasyUO 1.4.
 +
This has been fixed in EasyUO 1.5 however.''
  
gosub
+
===gosub===
 
The gosub command transfers the execution to a sub with the name given by the parameter.
 
The gosub command transfers the execution to a sub with the name given by the parameter.
  
Line 96: Line 89:
 
Parameters can be added after the sub name. They will be transfered in the variables %1, %2, and so on. The variable %0 holds the number of parameters passed.
 
Parameters can be added after the sub name. They will be transfered in the variables %1, %2, and so on. The variable %0 holds the number of parameters passed.
  
'''return'''
+
===return===
 
The '''return''' command transfers the execution back to where the sub was called using '''gosub'''.
 
The '''return''' command transfers the execution back to where the sub was called using '''gosub'''.
  
Line 102: Line 95:
 
return</pre>
 
return</pre>
  
'''goto'''
+
==goto==
 
The '''goto''' command moves the execution to another part of the script. The destination point is given by a label.
 
The '''goto''' command moves the execution to another part of the script. The destination point is given by a label.
  
 
<pre>
 
<pre>
 
goto label_name </pre>
 
goto label_name </pre>
Labels
+
===Labels===
 
A label specifies a point in the code where you can '''goto''' to.
 
A label specifies a point in the code where you can '''goto''' to.
  
 
<pre>
 
<pre>
 
label_name:</pre>
 
label_name:</pre>
 
<pre>
 
User Contributed Notes
 
 
'''/docs/control-structures.php'''</pre>
 

Revision as of 19:00, 24 August 2005

if

The “if” construct, one of the most important features of many languages including EasyUO, allows for conditional execution of code fragments. The basic syntax of the command is:

If ( expression )

{ statement }

As described in the section about Expressions, expression is evaluated as a Boolean value. If it evaluates to #true the statement will be executed.

The following code fragment will display a message box with the phrase "a is greater than b" if the value in the variable %a is greater than the value in the variable %b:

set %a 1
set %b 0
if %a > %b
	display ok a is greater 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 code block by using braces. For example, this code would display a message box with the phrase "a is greater than b" if the vaule in the variable %a is greater than the value in the variable %b, and would then assign the value of %a into the variable %b:

set %a 10
set %b 2
if %a > %b
{
	display ok a is greater than b
	set %b %a
}
halt

“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 or code block if a certain condition is met, and a different statement or code block if the condition is not met. This is the purpose of the else statement. Else extends an “if” statement to execute a statement or code block only in the instance that the expression of the previous 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:

set %a 10
set %b 20
if %a > %b
{
	display ok a is greater than b
}
else
{
	display ok a is less than b
}
halt

Note: in EasyUO 1.4 it is not possible to nest if commands and use else. The method around this limitation is to use an additional if to test for the opposite condition. In EasyUO 1.5, if and else can be safely nested however.

for

The “for” loop is a loop that iterates a variable either up or down upon execution of the code block that it controls. The code block is executed with each iteration until the second value is reached.

for variable from_value to_value
{
	statement
}

The loop starts by assigning the from_value to the variable, runs the statement(s) inside the code block. The variable is then incremented or decremented as required for the variable to be one integer value closer to the to_value. The statement inside the code block is executed after each and every iteration of the variable.

Note: in EasyUO 1.4, brackets are required to be used with a for statement, even if the code block consists only of a single statement. As of EasyUO 1.5, if the code to be executed with a for statement is only a single statement, the braces are optional.

Subs

The term sub is short for subroutine. Subs are an important feature of the EasyUO script, that will make your scripts more logically structured, easier to read, and will make you have to write less code.

A basic sub definition starts with a sub statement followed by the subs name (sub names are case insensitive), a number of script lines and ends with a return statement.

sub testSub
  ...
  return

If parameters were added to the calling gosub command they will be present in the variables %1, %2, and so on. The variable %0 holds the number of parameters passed. As all variables are in the global scope, the %0, %1, %2.. will be overwritten if you call another sub from inside a sub.

Warning: Do not GOTO out of a sub, always use return! EasyUO will probably not crash but it is very bad programming style.

Note: inline comments do not work with the sub command in EasyUO 1.4. This has been fixed in EasyUO 1.5 however.

gosub

The gosub command transfers the execution to a sub with the name given by the parameter.

gosub sub_name

Parameters can be added after the sub name. They will be transfered in the variables %1, %2, and so on. The variable %0 holds the number of parameters passed.

return

The return command transfers the execution back to where the sub was called using gosub.

return

goto

The goto command moves the execution to another part of the script. The destination point is given by a label.

goto label_name 

Labels

A label specifies a point in the code where you can goto to.

label_name: