.DT
if
$MUDNAME$ driver help
if
.SH Name
.SI 5
if() - the most basic conditional statement
.EI
.SH Synopsis
.SI 5
if( conditional statements )
{
action(s)
}
.EI
.SH Description
.SP 5 5
This might be the most essential driver efun of all. The if() efun evaluates any number of conditional statements and performs an action based on the result.
There are a number of optional syntaxes for the conditional statements:
Equality - ==. If the two sides are equal, the statement is true.
Or - ||. If EITHER of the conditions the || links together are true, the statment evaluates to be true.
And - &&. If BOTH of the conditions the && links together are true, the statement evaluates to be true.
Not - !. The ! takes a true statement and makes it false, or vice-versa.
Less than - <. True if the value on the left of the < is less than the value on the right.
Greater than - >. True if the value on the left of the > is greater.
Less than or equal to - <=. True if the value on the left of the <= is less than or equal to the value on the right.
Greater than or equal to - >=. True if the value on the left of the >= is greater than or equal to the value on the right.
Not equal to - !=. True if one value is not equal to the other.
For multiple statements, parentheses, (), can be used to separate the different clauses.
.EP
.SH Example 1
.SI 5
int changed;
changed = 1;
if( changed == 1 )
write("Changed is equal to 1!\n");
The result: "Changed is equal to 1!" is printed, because changed is equal to 1.
.EI
.SH Example 2
.SI 5
int changed;
changed = 0;
if( changed == 1 )
write("Changed is equal to 1!\n");
The result: Nothing happens, because changed is not equal to 1.
.EI
.SH Example 3
.SI 5
string str;
str = "I love pancakes!";
if( str == "lard" )
write("Someone here likes lard!\n");
The result: Nothing happens, because the string is not equal to "lard".
.EI
.SH Example 4
.SI 5
int x, y;
x = 1;
y = 0;
if( ( x == 1 ) || ( y == 1 ) )
write("I love frogs!\n");
else
write("I love cats!\n");
The result: "I love frogs!" is printed, because one of the two statements in that if statement, ( x == 1 ), is true. Remember that ||, OR, means that the if is true if either statement is true.
.EI
.SH Example 5
.SI 5
string str1, str2;
str1 = "Windy";
str2 = "Sandoz";
if( str1 == "Windy" && str2 == "Sandoz" )
write("I like armadillos!\n");
else
write("I like hamsters!\n");
The result: "I like armadillos!" is printed, because str1 is "Windy" AND str2 is "Sandoz".
.EI
.SH Example 6
.SI 5
int x, y;
x = 4;
y = -14;
if( x < y )
write("X is less than y.");
else
write("X is greater than y.");
The result: "X is greater than y." is printed, because, as I'm sure you know, x is greater than y. :)
.EI
.SH Example 7
.SI 5
int zip, zop;
zip = 13.67774;
zop = zip;
if( zip <= zop )
write("Zip is less than or equal to zop.");
else
write("Zip is greater than zop.");
The result: "Zip is less than or equal to zop." is printed.
.EI
.SH Example 8
.SI 5
int zip = 1;
if( !zip )
write("Zip is false.");
else
write("Zip is true.");
The result: "Zip is true." is printed. The ! symbol REVERSES zip. Since zip is 1, zip reversed is 0. if( 0 ) returns false.
.El
.SH Example 9
.SI 5
And, just to show you how complicated they can get...
int a, b, c, d, e, f;
a = 1;
c = a;
e = a;
b = 0;
d = b;
f = b;
if( ( ( !a == 4 ) && ( b == 7 ) ) || ( ( a == 9 ) && ( f == 3 ) ) &&
( ( !c == a ) || ( d == c ) ) && ( ( c < a ) || ( c > d ) ) &&
( d == b ) )
write("Whew, that took a while!\n");
else
write("All that work, and it returned false!\n");
The result: Let's see...carry the one, divide by two, add 17.34, ask someone for help...Ah, it evaluates to false. Just take my word on this one. :)
.EI
.SH Example 10
.SI 5
A really sneaky way to do a quick if-else combo is through the use of parenthesis. Take a look:
string str;
str = "oink";
write("I really feel like a "+ ( str == "oink" ? "pig" : "cow" ) +" today!\n");
The result: "I really feel like a pig today!" is printed. Look at the 'thing' in parentheses. It can be broken down into three parts:
( conditional ? true : false )
The "conditional" part is a normal if statement. The 'true' part is what to do if the conditional is true, the 'false' part is done if the conditional is false.
Note - This is the better way to do conditional statements. It's much easier on the CPU, and saves time and memory.
.EI
.SH See also
.SI 5
switch, else
.EI