Difference between revisions of "If"

From Wiki
Jump to: navigation, search
m
(added else statement)
Line 1: Line 1:
 
{{command header|Flow Control}}
 
{{command header|Flow Control}}
{{body|if ( expression ) { }|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.
+
{{body|if ( expression ) { }
  
As described in the section about expressions ''expression'' is evaluated to its boolean value. If it evaluates to [[true]] the statement will be executed.
+
:''or''
  
The following code fragment will display <b>a is bigger than b</b> if %a is bigger than %b:
+
if ( expression ) { } else { } |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 1
+
set %a 100
set %b 0
+
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, 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 statement group. 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 10
+
set %a 100
set %b 2
+
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>
''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.}}
+
<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.}}
  
 
==== Example ====
 
==== Example ====
Line 29: Line 42:
 
display ok You are farther East than you are South!
 
display ok You are farther East than you are South!
 
halt
 
halt
 +
}
 +
else
 +
{
 +
if #charposz > 100
 +
{
 +
display ok You are at a high altitude!
 +
halt
 +
}
 
}
 
}
 
</pre>
 
</pre>
  
 
{{footer}}
 
{{footer}}

Revision as of 11:50, 14 October 2005

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

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.

Example

if #charposx > #charposy
{
	display ok You are farther East than you are South!
	halt
}
else
{
	if #charposz > 100
	{
		display ok You are at a high altitude!
		halt
	}
}

See also

Main Page Documentation