Author Topic: Interesting Operators  (Read 2602 times)

I was scrolling through this page: http://docs.garagegames.com/torque-3d/official/content/documentation/Scripting/Overview/Syntax.html
and I found some interesting things.

Instead of something like
Code: [Select]
if(%admin)
{
%powerful = "yes";
}
else
{
%powerful = "not at all";
}
Or even
Code: [Select]
switch(%admin)
{
case 1:
%powerful = "yes";
case 0:
%powerful = "nopenopenope";
}

I can just use
Code: [Select]
%powerful = %admin ? "yes" : "nope";If admin is 1, powerful is "yes". If admin is 0, powerful is "no".

I did not know of this.

I also learned that % can be used to find the remainder of an division operation.
So like.  34 goes into 40 once, with a remainder of 6.
So echo(40 % 34); returns 6.
40 % 22 returns 18.
40 % 18 returns 4.  Cause 18 goes into 40 2 times, with a remainder of 4.

Of equal interest, though I didn't learn it from that particular page, I learned that-
instead of using %a++;  or %a = %a + 1; to increment %a by 1.
I can use %a += 1;  or if i want to increment it by five I can use %a += 5;
Similarly I can use all of the normal operators to do stuff like this-
if %a = 6 then:
%a += 5; sets %a to 11
%a -= 2; sets %a to 4
%a /= 3; sets %a to 2
%a *= 2; sets %a to 12
%a %= 5; sets %a to 1

Interesting stuff.  I hope it's useful to someone other than me.
« Last Edit: March 18, 2015, 04:49:38 PM by Tendon »

Quote
I also learned that % can be used to find the remainder of an division operation.

Yep, % is the modulus operator, in some cases.

Something else you might find interesting with switches. You can use an or operator like so:
Code: [Select]
switch$(%food)
{
    case "trout" or "cod" or "bass":
        %type = "fish";

    case "apple" or "banana":
        %type = "fruit";

    case "Cheese":
        %type = "dairy";

    default:
        %type = "unknown";
}
And
Code: [Select]
switch(%number)
{
    case 0:
        %result = false;

    case 1 or 2:
        %result = true;
}
Are both valid.

Oh that's cool!  I didn't know that.  Are there any other operators other than or?

Oh that's cool!  I didn't know that.  Are there any other operators other than or?

See section A1 of this reference: http://greekmods.webs.com/docs/Appendix%20A%20-%20Quick%20References.pdf

Of equal interest, though I didn't learn it from that particular page, I learned that-
instead of using %a++;  or %a = %a + 1; to increment %a by 1.
I can use %a += 1;  or if i want to increment it by five I can use %a += 5;
Similarly I can use all of the normal operators to do stuff like this-
if %a = 6 then:
%a += 5; sets %a to 11
%a -= 2; sets %a to 4
%a /= 3; sets %a to 2
%a *= 2; sets %a to 6
%a %= 5; sets %a to 1

In reality, these compile to the normal, straight forward method; %a++ turns into %a = %a + 1, so don't go thinking of it as a performance-wise optimization. It's purely appearance.


Multi-line comments don't work.
They did in original TGE, but for some reason badspot removed them...

Yes young padawan, we call those ternary operators.

Something else you might find interesting with switches. You can use an or operator like so:

i saw an or operator at some point and i never played around with it that's super-useful


The operators posted in OP are the same in most languages. Its rare not to have things like +=, %, and ? : in a language.

Code: [Select]
switch(%number)
{
    case 0:
        %result = false;

    case 1 or 2:
        %result = true;
}

I did not know about this though, there's been times where i had to change from a switch statement to if elseif elseif etc statements because i wanted ors. This will be so loving useful.

Its rare not to have things like +=, %, and ? : in a language.
The last is implemented in different ways in many languages
For example, both VB.NET and SQL have a function rather than an operator: if(expression,truepart,falsepart)