Another type of conditional statement is a switch statement, but I do not yet know what these do.
A switch statement? Think of it like this:
if(%a == )
else if (%a ==)
else if (%a ==)
....
A switch statement does the same thing, comparing a single value against many different ones, and then donditionally doing one of them.
switch(%number)
{
case 1:
echo("One!");
case 2:
echo("Two!");
case 5:
echo("Five!");
default:
echo("Anythingelse!");
}
"default" is a special case(lol), it may be left out, but if it is used, it must occur last. It acts like a plain "else".
switch() treats the statement in the brackets like a number. If you want to use strings, use switch$().
Also, a very ugly form of an if is ?:. It's advantage is that you can use it within an expression. Using more than one might break things, though.
Example:
function servercmdAmIAdmin(%client)
{
messageclient(%client, '', "You" SPC ((%client.admin || %client.superAdmin) ? "are" : "are not") SPC "an admin.");
}
There is a reason nobody uses them, and that is because this is Blockland, not the
International Obfuscated C Code Contest, and they are very good at making code ugly and hard to read, and could easily get an add-on failed just for being there. It is far better to use an if() and store the result in a local variable than use a ?:. ?: is similar to using multiple %i++ in a single statement: There are far better ways to do it, it is easy to use wrong and cause accidental bugs, and it might look suspiciously like malicious code. It works, but just like goto used in C, there are *very* few, if any, situations where you might need it. (I have never, in all the years I have known C, needed a goto, although I can see where it might be useful in some hypothetical situations)
Also:
The following are considered true:
- Any number that isn't *exactly* zero (Even the result of devision by zero).
- true
- "true"