Operators

In this lesson we'll review all the logical operators that are available to use in C++.  Operators are special expessions that can be used to determine the relationship of two values.  They are often used in "if" statements to execute code when a condition is met. Operators in C++, as you will see, are really not much different from Lua at all.

Equals

The equals operator is denoted by two equals signs in a row:

==

The equals operator is a test to see if two values are equal.  This does not assign a value to a variable.

#include "Leadwerks.h"

int main() NEEDS ARGUMENTS HERE

{

int n=2;

if (n==2)

{

        Print("n equals 2")

}

}

The equals operator can be used with any data type, including strings and boolean values.

std::string name="Fred";

if (name=="Fred")

{

Print("The name's Fred");

}

Less Than

The less than operator determines if one number is smaller than another.  The less than sign looks a little bit like a bird's beak if you picture the bird facing to the left:

<

Remember, birds don't eat a lot, so the number where his beak points is smaller.  Here's an example of it in action:

int a = 1;

if (a < 2)

{

Print("a is less than 2");

}

Greater Than

The greater than operator determines if one number is larger than another.  The greater than sign looks a little bit like an alligator's mouth if you picture the alligator facing to the left:

>

Remember, alligators eat a lot, so the number where his mouth points is bigger.  Here's an example of it in action:

int a = 1;

if (a > 0)

{

Print("a is greater than 0");

}

Less Than or Equal To

This is a variation of the less than operator that will also be true if the values are equal. It is denoted by a less than sign followed by a single equals sign:

<=

Here is an example of its usage:

int a = 1;

if (a <= 1)

{

Print("a is less than or equal to 1");

}

Greater Than or Equal To

This is a variation of the greater than operator that will also be true if the values are equal.   It is denoted by a greater than sign followed by a single equals sign:

>=

Here is an example of its usage:

int a = 1;

if (a >= 1)

{

Print("a is greater than or equal to 1");

}

Not Equal

This operator is the opposite of the equals operator.  It is indicated by a tilde character followed by a single equals sign:

!=

Here is an example of its usage:

int a = 1;

if (a != 5)

{

Print("a does not equal 5");

}

Think of the exclaimation mark as someone yelling "NOT!" right before the equals sign. You can even use the "NOT!" sign by itself with a boolean variable to see if it equals false:

bool b = false;

if (!bool)

{

Print("b is false");

}

Add One

C++ adds another operator we can make use of. If we type "++" at the end of a variable we can add one to the variable very easily:

int n=1;

n++;

Print(n);

The above code will print out "2".

Subtract One

We can also subtract one from a variable by typing "--" at the end of a variable:

int n=1;

n--;

Print(n);

The above code will print out "0". Pretty cool, huh?

Add Anything

If we type "+=" at the end of a variable we can add whatever is to the right of a variable very easily:

int n=1;

n+=3;

Print(n);

The above code will print out "4". Note that "+=1" and "++" will do the same thing.

This is just a faster way of typing "n = n + 3;".

Subtract Anything

If we type "-=" at the end of a variable we can also subtract whatever is to the right of a variable very easily:

int n=3;

n-=2;

Print(n);

The above code will print out "1". Note that "-=1" and "--" will do the same thing.

This is just a faster way of typing "n = n - 3;".

Conclusion

You will frequently encounter all of these operators in C++ code.  Make sure you understand how they all work, and ask on the forum if you have any questions.