Difference between revisions of "Control Structures"

From Wiki
Jump to: navigation, search
 
m
 
(11 intermediate revisions by 5 users not shown)
Line 1: Line 1:
'''if'''
+
[[Category: Language Reference]]
 +
__TOC__
 +
==Goto's==
 +
Goto's are an archaic and powerful command to automatically move to a different point in a program. They never [[return]] unless another goto is used. They should <b>never</b> be used to return from a subroutine.
  
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:
+
===label===
 +
A [[label]] is a marker for the point a goto command will jump to. These are created by using any word that isn't a command, and suffixing it with a colon, like this...
 +
<pre>mylabel:</pre>
  
<pre>
+
===goto===
if ( expression )
+
A [[goto]] is used to jump directly to a label as defined above. This is accomplished by using [[goto]] {label}
statement</pre>
+
<pre>goto mylabel</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.
+
{{note|You must never use goto to jump out of a sub! Use return to properly terminate a sub routine. To prevent a stack overflow, EUO only supports 1000 consecutive GoSubs without returning. Remember this when using recursion! When the GoSub stack is about to get 1001 levels, the very first level in the bottom of the stack is deleted to make room.}}
  
The following code fragment will display 'a is bigger than b' if %a is bigger than %b:
 
  
<pre>
+
==Subs==
initEvents
+
The term <i>sub</i> 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.
  
set %a 1
+
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.
set %b 0
+
if %a > %b
+
event SysMessage a is bigger than b</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:
+
  
 
<pre>
 
<pre>
initEvents
+
sub testSub
 +
  ...
 +
  return</pre>
  
set %a 10
+
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.
set %b 2
+
if %a > %b
+
{
+
event SysMessage a is bigger than b
+
set %b %a
+
}</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.
+
===gosub===
 +
The gosub command transfers the execution to a sub with the name given by the parameter.
  
'''else'''
+
<pre>
 +
gosub sub_name</pre>
  
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:
+
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.
 
+
<pre>
+
initEvents
+
  
set %a 10
+
===return===
set %b 20
+
The '''return''' command transfers the execution back to where the sub was called using '''gosub'''.
if %a > %b
+
{
+
  event SysMessage a is bigger than b
+
}
+
else
+
{
+
  event SysMessage a is NOT bigger than b </pre>
+
  
 
<pre>
 
<pre>
'''Note'''
+
return</pre>
Right now it is not possible to nest if commands and use else. Only one if, if you are using else.</pre>
+

Latest revision as of 11:17, 3 August 2007

Goto's

Goto's are an archaic and powerful command to automatically move to a different point in a program. They never return unless another goto is used. They should never be used to return from a subroutine.

label

A label is a marker for the point a goto command will jump to. These are created by using any word that isn't a command, and suffixing it with a colon, like this...

mylabel:

goto

A goto is used to jump directly to a label as defined above. This is accomplished by using goto {label}

goto mylabel
Note: You must never use goto to jump out of a sub! Use return to properly terminate a sub routine. To prevent a stack overflow, EUO only supports 1000 consecutive GoSubs without returning. Remember this when using recursion! When the GoSub stack is about to get 1001 levels, the very first level in the bottom of the stack is deleted to make room.


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.

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