Objective C Tutorial - Objective-C Operators






Objective-C language has the following operators.

  • Arithmetic Operators

  • Relational Operators

  • Logical Operators

  • Bitwise Operators

  • Assignment Operators

  • Misc Operators





Arithmetic Operators

The following table shows all the arithmetic operators in Objective-C.

Operator Description
+ Adds two operands
- Subtracts second operand from the first
* Multiplies both operands
/ Divides numerator by denominator
% Modulus Operator and remainder of after an integer division
++ Increment operator increases integer value by one
-- Decrement operator decreases integer value by one




Relational Operators

The following table lists the relational operators supported by Objective-C.

Operator Description
== Checks if the values of two operands are equal or not.
!= Checks if the values of two operands are not equal.
> Checks if the value of left operand is greater than the value of right operand.
< Checks if the value of left operand is less than the value of right operand.
>= Checks if the value of left operand is greater than or equal to the value of right operand.
<= Checks if the value of left operand is less than or equal to the value of right operand.

Logical Operators

The following table lists the logical operators supported by Objective-C language.

Operator Description
&& Logical AND operator. If both the operands are non zero then condition becomes true.
|| Logical OR Operator. If any of the two operands is non zero then condition becomes true.
! Logical NOT Operator. Reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make false.

Bitwise Operators

Bitwise operator works on bits and perform bit by bit operation.

The truth tables for &, |, and ^ are as follows:

p q p & q p | q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

The Bitwise operators supported by Objective-C language are listed in the following table.

Operator Description
& Binary AND Operator copies a bit to the result if it exists in both operands.
| Binary OR Operator copies a bit if it exists in either operand.
^ Binary XOR Operator copies the bit if it is set in one operand but not both.
~ Binary Ones Complement Operator is unary and has the effect of 'flipping' bits.
<< Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.
>> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.

Assignment Operators

There are following assignment operators supported by Objective-C language:

Operator Description
= Assignment operator, Assigns values from right side operands to left side operand
+= Add AND assignment operator, It adds right operand to the left operand and assigns the result to left operand
-= Subtract AND assignment operator, It subtracts right operand from the left operand and assigns the result to left operand
*= Multiply AND assignment operator, It multiplies right operand with the left operand and assigns the result to left operand
/= Divide AND assignment operator, It divides left operand with the right operand and assigns the result to left operand
%= Modulus AND assignment operator, It takes modulus using two operands and assigns the result to left operand
<<= Left shift AND assignment operator
>>= Right shift AND assignment operator
&= Bitwise AND assignment operator
^= bitwise exclusive OR and assignment operator
|= bitwise inclusive OR and assignment operator

sizeof Operator

sizeof() returns the size of an variable. sizeof(anInteger) will return 4.

Conditional Expression

value = condition ? X:Y is called the Conditional Expression.

If Condition is true ? Then assign X to value, Otherwise assign Y to value.

Conditional operator ? : can be used to replace if...else statements. It has the following general form:

Exp1 ? Exp2 : Exp3;

Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the colon.

The value of a ? expression is determined like this: Exp1 is evaluated. If it is true, then Exp2 is evaluated and becomes the value of the entire ? expression. If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the expression.

Address Operator

& returns the address of an variable. &a; will give actual address of the variable.

Pointer * operator

* is the Pointer operator to a variable. *a; will pointer to a variable.