Expressions and Operators - FREE Guru Level Training For Beginners (2014)

FREE Guru Level Training For Beginners (2014)

Chapter 8: Expressions and Operators

“Finding a programmer to work with if you don't already know one will be a challenge. Merely judging if a programmer is exceptional vs. competent will be very hard if you are not one yourself. When you do find someone, work together informally for a while to test your compatibility.”- Jessica Livingston

In this chapter, you will learn about expressions and operators, including:

· Assignment operator

· Arithmetic operators

· Compound assignment operators

· Increment and decrement operators

· Comparison operators


The components of an expression is as follows: one operator, one assignment and two operands. Together these form an expression. Look at the following example:

int newresult = 1 + 2;

In this example + is the operator while 1 and 2 are the operands while newresult is the integer variable. You can also use variable and constants instead of operands as well.

Assignment Operator


In the example given above = is the most basic form of an operator. It takes two operands and adds, multiplies, subtracts or divides these and the result is the assignment of an integer to newresult, the variable. Here are a few examples of how the assignment operator can be used:

int a; // can be used to declare a variable

a = 10; // assignment of the value 10 to a variable, x

a = y + z; // the result is a value assigned to the integer a

a = y; // the value of the variable y is assigned to variable x


The same value can be assigned to various variables as well. Here’s how this can be done:

int a, y, z;

a = y = z = 50;

So, the variables a, y and z are chained together and the value of each variable is 50.


Arithmetic Operators


If you are/were a mathematics student, you’ll know of the basic arithmetic operators: multiplication (*), addition (+), division (/) and subtraction (-). These are said to be binary operators.

It is possible for you to use more than one arithmetic operators at the same time. Here’s an example:x = y - 10 * z + 2/ 4;

The result is not calculated from the left or the right. However, the end result is dependent upon operator precedence. If you intend on using more than one arithmetic operator at the same time, check out the following link to get an idea of how these work:http://www.techotopia.com/index.php/Objective-C_2.0_Operator_Precedence.

Compound Assignment Operators


In Objective C you can also combine assignments with logical/mathematical operations. This is usually done when a programmer wants to store the result of two operands inside one operand. Here’s how you can do this:

x = x + a;

The values of x and a (two operands) will be stored in the variable x.

Increment and Decrement Operators

Since these operators only work on one operand, they are also referred to as unary operators. Let’s see how this works:

x = x + 2; // value of variable x is increased by 2

x = x - 2; // value of variable x is decreased by 2

Another way to go about this is as follows:

x++; Increment of variable x by 1

x--; Decrement of variable x by 1

Be careful of where you put the operators. Putting the increment and decrement operators before the variable means the increment or decrement will be performed before other operations are performed on the variable x.

Comparison Operators

These will return Boolean values which means you will either get a value of true or false and that depends on the comparison result. Since comparison operators work on two operands they are known as binary operators and can be used for flow control when it comes to constructing a program. Consider this example:

BOOL result;

int a = 20;

int b = 80;

result = a < b;

Since a actually is less than b (a being equal to 20 and b being equivalent to 80), you will get a value of true.

Summary

This chapter covers some of the most basic types of operators.