# Basic Logic Operators

This is pretty simple.

Logic is stuff we perform on variables. This is either tests or actual operations.

A test would be

if (x == 1)
{
y = 10;
b = d;
}
else
{
y = 5;
b =d –5;
}

Now, there is some structure there that needs to be looked at. Firstly the actual decision maker, the IF statement has brackets around the operators. The reason for this is that operation is literally being broken down into a true of false statement.

With an if statement, you have a binary situation. Either x > 0, or it isn’t. It doesn’t matter how complex you make the decision maker, ultimately it comes down to a yes or no situation.

Now, since this is true, you can take a short cut here.

if (x != 0)

Could be written as

if (x)

since the statement “is not equal to zero” is implied, or assumed. We’ll talk a bit more about this in a second.

Notice also that the code we want to run if the if statement is true is contained within brackets. This indicates to the compiler the scope of the code to run. In this case we want to run the code y = 10 and b = d.
This is common to all of C, and operators for procedures as well as inline code.

You can nest if/then/else statements as much as you like.
Notice the indentation – this is make visually cuing which code is associated with which if statement that much easier.

The brackets are not strictly speaking necessary if you are only running one line of code after an if statement.

if (x == 1)
{
d = 10;
}

is the same objectively as

if (x==1)
d = 10;

or

if (x==1) d = 10;

I tend to go with the first version, since although it may bloat the code, it makes adding more statements to that if structure easier, since the brackets are already there.

Now, back to those decision makers.

Notice that we use two equal signs when doing a test, but only one when using an assignation.

x == 1 returns a 1 or a 0 depending on whether its true or not. X = 1 sets x to the value of 1. Big difference. X = 1 is always true, and returns a true value, so the code

if (x=1)
{
blah = 2;
}

will ALWAYS set blah to 2, since x is always set to 1. Be careful about this, since the compiler won’t see this or warn you. I make this mistake a lot.

The test operators are;-

== equals

!= not equal

< less than

> greater than

<= less than of equal to

>= greater than or equal to

Other stuff to be aware of.

When you do an if statement, you can use logical operators.
I’m assuming you know what a logical AND / OR does. You can figure it out from the code anyway:)

The code

if ( (x == 1) && (s == e))
{
blah
}

is saying “Ok, if x is equal to 1 AND s is equal to e, then do blah.” We use two &&’s to signify the AND function.
In this case both of the decisions have to be true before blah is executed.

However, if you were doing a logical AND as a data assignation, then you would only use one, like the equals signs, so

a = a & 1;

Would be a equals a AND 1, so a would either equal a zero or a one depending on it’s initial value. Talk to me if this is unclear.

Similarly the logical OR operation works the same way, except you use the | symbol to denote an OR operation. That’s the two vertical lines on your keyboard.

if ( (x == 1) || (s == e))
{
blah;
}

means that if either x equals 1 OR s equals e, blah gets executed. Similarly

a = a | 1;

would set bit zero of a. If a was a 1 to begin with, then it’s still a 1. If it was 0 then now it’s a one.

There is XORing but we don’t need to address that right now.

Notice how the brackets are set up. Nested brackets get executed inner most first. So, for the first example, the x == 1 gets done first, then the s == e test, the after that the comparison/AND/OR between them gets done last. This assures that everything is broken down properly internally, and is just easier to read.

You can do a negative test on everything. For example

if (!(x == 1))
{
blah;
}

is the same as

if (x !=1)

in practical execution.

The difference is that in the first example the NOT is performed on the result of the test, where as in the second example, we are inverting the conditions of the actual test itself. See the difference?

Remember the short cut

if (x)

Well, you can take that a stage further with

if (!x)

The first example is short for “if x is not zero or NULL”, the second is short for “if x is zero or NULL”.

When we perform math on variables there is some stuff to bear in mind. While you can do a divide on an integer, the result WILL get rounded down to the nearest whole number.

You can use brackets inside of actual math, just like you would do in real life. This follows the same rules as we described earlier – innermost brackets get done first.

For example

a = b + ((c * d) + (c +1));

Would result in a being equal to c * d plus c plus 1 plus b. There are some short cuts

To increment a variable by just 1 you can write it

a++;

Similarly you can decrement a variable by 1 by

a–;

This is called post-decrement/increment. This means that the variable has 1 added /  subtracted AFTER the variable is used. To explain;

a = 1;
b = array[a++];

Would mean that b would be equal to the value in the array cell position 1. Immediately after the assignation of b is done, a is incremented, becoming 2.

You can do pre-increment/decrement by placing the ++ or – at the front of the variable, so

a = 1;
b = array[++a];

Is different from the above code in that b would equal the value in the array at position 2, since a was incremented BEFORE it was used.

One last shortcut.

If the variable you are assigning is the same as the one having an operation performed on it, you can shortcut the code.

A += b;
Is the same as a = a + b;

It’s just a shorter way of writing it.

Same as
a -= d;
Equals  a = a – d;

And
a *=3
Is a = a * 3;

And so on.

One last thing.

You can mix and match floats and ints in tests. For example

float x = 10.0f;

int s = 20;

If (x < s)
{
blah;
}

will work. What happens in execution is that the variable is actually converted to a float for the conditions of the test. BE CAREFUL with this, since a float/integer or integer/float conversion is NOT free, and if you’re doing it a lot can be costly.

Don’t worry if you don’t get all of this instantly.
Refer back to this when you get to the practical, since it will all become more apparent at that time what is meant here.