Objective-C Operators , Objective-C also provides a set of so called logical operators designed to return boolean true and false. The OR (||) operator returns 1 if one of its two operands evaluates to true, otherwise it returns 0
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.
Arithmetic Operators
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
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
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
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. |
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 |
Assignment Operators
= | 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 |
Compound Assignment Operators
Operator | Description |
x += y | Add x to y and place result in x |
x -= y | Subtract y from x and place result in x |
x *= y | Multiply x by y and place result in x |
x /= y | Divide x by y and place result in x |
x %= y | Perform Modulo on x and y and place result in x |
x &= y | Assign to x the result of logical AND operation on x and y |
x |= y | Assign to x the result of logical OR operation on x and y |
x ^= y | Assign to x the result of logical Exclusive OR on x and y |
Increment and Decrement Operators
x = x + 1; // Increase value of variable x by 1
x = x - 1; // Decrease value of variable x by 1
x++; Increment x by 1
x--; Decrement x by 1
Comparison Operators
if (x == y)
// Perform task
Boolean Logical Operators
bool flag = true; //variable is true
bool secondFlag;
secondFlag = !flag; // secondFlag set to false
if ((10 < 20) || (20 < 10))
NSLog (@"Expression is true");
if ((10 < 20) && (20 < 10))
NSLog (@"Expression is true");
The Ternary Operator
[condition] ? [true expression] : [false expression]
int x = 10;
int y = 20;
NSLog(@"Largest number is %i", x > y ? x : y );
Happy Coding 🙂