The Objective-C language is developed on top of C Programming. The operator in Objective-C is the same as the C language operators. It is primarily used in developing iOS and MacOS operating systems and software applications for iOS. Operators are used to forming a mathematical expression using variables. To perform an operation on a variable we used an operator. An operator is a symbol that tells the compiler which operation to perform. For example, c = a + b, here +, = is the operator and a, b, and c are operands. an operator is used to perform operations on operands.
Types of Operators in Objective-C
- Arithmetic Operators
- Relational Operators or Comparison operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- other/Misc Operators
Arithmetic Operators
To perform arithmetic/mathematical operations on operands. We used Arithmetic Operators. i.e. addition(+), subtraction(-), Multiplication(*), Division(/), etc.
Operator | Description | Example |
---|
+ | Perform the addition of two operands | int a=5, b=7, c; c = a + b ; // c=12 |
- | Perform the subtraction of two operands | int a=5, b=7, c; c = a - b ; // c=-2 |
* | Perform the multiplication of two operands | int a=5, b=7, c; c = a * b ; // c=35 |
/ | Perform the division operation numerator by the denominator | int a=20, b=5, c; c = a / b ; // c=4 |
% | Perform the division and return the remainder | int a=20, b=5, c; c = a % b ; // c=0 |
++ | Increment operand value by 1 | int a=5; a++; // after increment a=6 |
-- | Decrement operand value by 1 | int a=6; a--; // after decrement a=5 |
Example:
ObjectiveC
// Objective-C program of Arithmetic operations
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Initializing two numbers
int n1 = 5, n2 = 3;
// Printing addition
NSLog(@"ADDITION = %d\n", (n1+n2));
// Printing subtraction
NSLog(@"SUBTRACTION = %d\n", (n1-n2));
// Printing multiplication
NSLog(@"MULTIPLICATION = %d\n", (n1*n2));
// Printing division
NSLog(@"DIVISION = %d\n", (n1/n2));
// Printing mod
NSLog(@"MODULUS = %d\n", (n1%n2));
// Printing n1 with increment by 1
NSLog(@"INCREMENT = %d\n", n1++);
// Printing n1 with decrement by 1
NSLog(@"DECREMENT = %d\n", n1--);
[pool drain];
return 0;
}
Output:
2022-12-10 17:35:46.749 jdoodle[25:25] ADDITION = 8
2022-12-10 17:35:46.750 jdoodle[25:25] SUBTRACTION = 2
2022-12-10 17:35:46.750 jdoodle[25:25] MULTIPLICATION = 15
2022-12-10 17:35:46.750 jdoodle[25:25] DIVISION = 1
2022-12-10 17:35:46.750 jdoodle[25:25] MODULUS = 2
2022-12-10 17:35:46.750 jdoodle[25:25] INCREMENT = 5
2022-12-10 17:35:46.750 jdoodle[25:25] DECREMENT = 6
Relational Operators
Relational operators or Comparison operators are used for the comparison of two values and finding the difference between two values. i.e Equals to ( == ), Greater than ( > ), Less than ( < ) and etc.
Operator | Description | Example |
---|
== | Comparing the values of two operands are equal or not if equal then the condition becomes true else becomes false. | int a=5,b=7; (a==b) // false |
!= | Comparing the values of two operands are equal or not if values are not the same, then the condition becomes true else becomes false. | int a=5, b=7 (A!= B) // True |
> | Compare the value of the left operand is greater than the value of the right operand. if the greater value, then the condition becomes true else becomes false. | int a=7,b =5; (a>b) // true |
< | Compare the value of the left operand is less than the value of the right operand. if less value, then the condition becomes true else becomes false. | int a=5,b=7; (a<b) // true |
>= | Compare the value of the left operand to is greater than or equal to the value of the right operand. if a greater or equal value, then the condition becomes true else becomes false. | int a=7,b =7; (a>=b) // true |
<= | Compare the value of the left operand to is less than or equal to the value of the right operand; if less or equal value, then the condition becomes true else becomes false. | int a=5,b=5; (a<=b) // true |
Example:
ObjectiveC
// Objective-C program of Relational Operations
// or Comparison operations
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Initializing two numbers
int n1 = 5, n2 = 3;
// Return true if both numbers are same else return false
NSLog(@"Equal = %d\n", (n1==n2));
// Return true if both numbers are not same else return false
NSLog(@"NOT Equal = %d\n", (n1!=n2));
// Return true if n1 is less than n2 else return false
NSLog(@"LESS THAN = %d\n", (n1<n2));
// Return true if n1 is greater than n2 else return false
NSLog(@"GREATER THAN = %d\n", (n1>n2));
// Return true if n1 is greater that or equals to n2 else return false
NSLog(@"LESS THAN EQUALS = %d\n", (n1>=n2));
// Return true if n1 is less than or equals to n2 else return false
NSLog(@"GREATER THAN EQUALS = %d\n", (n1<=n2));
[pool drain];
return 0;
}
Output:
2022-12-06 15:46:26.041 a.out[1072:1072] Equal = 0
2022-12-06 15:46:26.043 a.out[1072:1072] NOT Equal = 1
2022-12-06 15:46:26.043 a.out[1072:1072] LESS THAN = 0
2022-12-06 15:46:26.043 a.out[1072:1072] GREATER THAN = 1
2022-12-06 15:46:26.043 a.out[1072:1072] LESS THAN EQUALS = 1
2022-12-06 15:46:26.043 a.out[1072:1072] GREATER THAN EQUALS = 0
Logical Operators
Logical operators are used to perform logical operations on operands. it returns either 0 or 1.
Operator | Description | Example |
---|
&& | If both the conditions are true then the condition becomes true. | (1 && 1) // true (1 && 0) // false (0 && 1) // false (0 && 0) // false |
|| | If any one condition is true out of two conditions then the condition becomes true | (1 || 1) // true (1 || 0) // true (0 || 1) // true (0 || 0) // false |
! | If the condition is true it will return false and vice versa. | !(1 && 1) // false !(1 && 0) // true !(0 && 1) // true !(0 && 0) // true |
Example:
ObjectiveC
// Objective-C program of Logical Operations
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Initializing two numbers
int n1 = 1, n2 = 1;
// Return 1 if both condition are true else return 0
NSLog(@"LOGICAL AND = %d\n", (n1&&n2));
// Return 1 if either of one or all conditions are true else return 0
NSLog(@"LOGICAL OR = %d\n", (n1||n2));
// Return 1 if condition is false else return 0
NSLog(@"LOGICAL NOT = %d\n", (!n1));
[pool drain];
return 0;
}
Output:
2022-12-06 16:01:31.808 a.out[1514:1514] LOGICAL AND = 1
2022-12-06 16:01:31.812 a.out[1514:1514] LOGICAL OR = 1
2022-12-06 16:01:31.812 a.out[1514:1514] LOGICAL NOT = 0
Bitwise Operators
The bitwise operators are used to perform the operations on the bit level of operands. When we perform bitwise operations, It consists of two digits, either 0 or 1. It is mainly used for reduced heavy mathematical operations.
Operator | Description | Example |
---|
& | Perform the AND operation between bits of both operands. | int a=5,b=7; (a & b) // 0101 i.e. 5 |
| | Perform the OR operation between bits of both operands. | int a=5,b=7; (a | b) // 0111 i.e. 7 |
^ | Perform the XOR operation between bits of both operands. | int a=5,b=7; (a ^ b) // 0010 2 |
~ | Perform the Ones' complement of the operand (complementing bits). | int a=5; ~a // 1010 10 |
<< | Shifting bits of the left operand to the left side by the right operand value | int a=5; a << 2 // 10100 i.e. 20 |
>> | Shifting bits of the left operand to the right side by the right operand value. | a >> 2 // 001 i.e. 1 |
Example:
ObjectiveC
// Objective-C program of Bitwise Operations
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// n1 = 5(00000101), n2 = 9(00001001)
int n1 = 5, n2 = 9;
// The result is 00000001
NSLog(@"Bitwise AND(n1&n2) = %d\n", (n1&n2));
// The result is 00001101
NSLog(@"Bitwise OR(n1|n2) = %d\n", (n1|n2));
// The result is 00010010
NSLog(@"LEFT SHIFT(n2<<2) = %d\n", (n2<<2));
// The result is 00000100
NSLog(@"RIGHT SHIFT(n2>>2) = %d\n", (n2>>2));
// The result is 00001100
NSLog(@"XOR(n1^n2) = %d\n", (n1^n2));
// The result is 11111010
NSLog(@"ONCE COMPLIMENT(~n1) = %d\n", (~n1));
[pool drain];
return 0;
}
Output:
2022-12-06 16:26:01.001 a.out[1091:1091] Bitwise AND(n1&n2) = 1
2022-12-06 16:26:01.005 a.out[1091:1091] Bitwise OR(n1|n2) = 13
2022-12-06 16:26:01.005 a.out[1091:1091] LEFT SHIFT(n2<<2) = 36
2022-12-06 16:26:01.005 a.out[1091:1091] RIGHT SHIFT(n2>>2) = 2
2022-12-06 16:26:01.005 a.out[1091:1091] XOR(n1^n2) = 12
2022-12-06 16:26:01.005 a.out[1091:1091] ONCE COMPLIMENT(~n1) = -6
Assignment Operators
Assignment operators are used to assign value to an operand/variable. The left side is the operand and the right side is the value of that operand which assign using the assignment operator (=).
Operator | Description |
---|
= | Assign values from right-side operands to the left-side operand. |
+= | Perform the addition of the right operand to the left operand and assign the result to the left operand. |
-= | Perform subtraction of the right operand from the left operand and assign the result to the left operand. |
*= | Perform the multiplication of the right operand with the left operand and assign the result to the left operand. |
/= | Perform the division of the left operand with the right operand and assign the result to the left operand. |
%= | Perform the division of the left operand with the right operand and assigns the result i.e. remainder to the left operand. |
<<= | Left shift & assignment operator. |
>>= | Right shift & assignment operator. |
&= | Bitwise AND & assignment operator. |
Example:
ObjectiveC
// Objective-C program of Assignment Operations
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int n1 = 20;
int n2;
// Using assignment operator
n2 = n1;
NSLog(@"ASSIGN(=)= %d\n", n2);
// Using short hand addition operator
// n1=n1+n2
n1 += n2;
NSLog(@"SHORT HAND ADDITION(+=)= %d\n", n1);
// Using short hand subtraction operator
// n1=n1-n2
n1 -= n2;
NSLog(@"SHORT HAND SUBTRACTION(-=)= %d\n", n1);
// Using short hand multiplication operator
// n1=n1*n2
n1 *= n2;
NSLog(@"SHORT HAND MULTIPLICATION= %d\n", n1);
// Using short hand division operator
// n1=n1/n2
n1 /= n2;
NSLog(@"SHORT HAND DIVISION(/=)= %d\n", n1);
n1 = 100;
// Using short hand mod operator
// n1=n1%n2
n1 %= n2;
NSLog(@"n1 MOD n2(%=)= %d\n", n1);
// Using short hand bitwise AND operator
// n1=n1&n2
n1 &= 2;
NSLog(@"SHORT HAND BITWISE AND(&=)= %d\n", n1);
// Using short hand XOR operator
// n1=n1^n2
n1 ^= 2;
NSLog(@"SHORT HAND XOR(^=)= %d\n", n1);
// Using short hand bitwise OR operator
//n1=n1|2
n1 |= 2;
NSLog(@"SHORT HAND BITWISE OR(|=)= %d\n", n1);
// Using short hand left shift operator
// n1=n1<<2
n1 <<= 2;
NSLog(@"SHORT HAND LEFT SHIFT(<<=)= %d\n", n1);
// Using short hand right shift operator
// n1=n1>>2
n1 >>= 2;
NSLog(@"SHORT HAND RIGHT SHIFT(>>=)= %d\n", n1);
[pool drain];
return 0;
}
Output:
2022-12-06 16:44:09.028 a.out[4481:4481] ASSIGN(=)= 20
2022-12-06 16:44:09.031 a.out[4481:4481] SHORT HAND ADDITION(+=)= 40
2022-12-06 16:44:09.031 a.out[4481:4481] SHORT HAND SUBTRACTION(-=)= 20
2022-12-06 16:44:09.031 a.out[4481:4481] SHORT HAND MULTIPLICATION= 400
2022-12-06 16:44:09.031 a.out[4481:4481] SHORT HAND DIVISION(/=)= 20
2022-12-06 16:44:09.031 a.out[4481:4481] n1 MOD n2(%=)= 0
2022-12-06 16:44:09.031 a.out[4481:4481] SHORT HAND BITWISE ANS(&=)= 0
2022-12-06 16:44:09.031 a.out[4481:4481] SHORT HAND XOR(^=)= 2
2022-12-06 16:44:09.031 a.out[4481:4481] SHORT HAND BITWISE OR(|=)= 2
2022-12-06 16:44:09.031 a.out[4481:4481] SHORT HAND LEFT SHIFT(<<=)= 8
2022-12-06 16:44:09.031 a.out[4481:4481] SHORT HAND RIGHT SHIFT(>>=)= 2
Misc Operators or Special Operators
These operators are special operators provided by the programming language that is used to perform a specific task.
Operator | Description | Example |
---|
sizeof() | The in-built function returns the size of a variable. | int a=10; sizeof(a); // 4byte |
& | It returns the address of a variable. | &a // address of a will return |
* | (asterisks) Used to create pointer variable. | int a=10; int *b = &a; |
?: | Conditional Expression. Alternate of simple if else statement. | int number1 = 5, number2 = 10, max; // max = 10 max = (number1 > number2) ? number1 : number2; |
Example:
ObjectiveC
// Objective-C program of Misc (sizeof & ternary) Operations
// or special operators
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Initialize numbers
int num1 = 7;
int res = 0;
// Pointer variable
int *ptr;
// Printing size of num1 of type int
NSLog(@"Size of num1(int)= %d\n", sizeof(num1));
// Assigning address of num1 to the pointer variable ptr
ptr = &num1;
NSLog(@"num1 = %d\n", num1);
NSLog(@"*ptr = %d\n", *ptr);
int a = 12;
// Checking condition(a==1) with ternary operator
res = (a == 10) ? 1: 0;
NSLog(@"Answer = %d\n", res);
res = (a == 12) ? 1: 0;
NSLog(@"Answer = %d\n", res);
[pool drain];
return 0;
}
Output:
2022-12-06 17:05:01.804 a.out[248:248] Size of num1(int)= 4
2022-12-06 17:05:01.805 a.out[248:248] num1 = 7
2022-12-06 17:05:01.805 a.out[248:248] *ptr = 7
2022-12-06 17:05:01.805 a.out[248:248] Answer = 0
2022-12-06 17:05:01.805 a.out[248:248] Answer = 1
Operators Precedence
Operator precedence tells which operation is performed first in an expression according to their priority and Associativity. Associativity is only used when there are two or more operators are present in the same expression. For example, m = 5 - 2 / 2, so here m is assigned with 4, not 1 because the / operator has high precedence than -. So first we divide 2/2 and then subtract 5-1.
All operators with the same precedence have the same associativity. Associativity can be either Left to Right or Right to Left. The following table shows the operator precedence:
Operator | Associativity |
---|
() [] -> . ++ - - () | Left to right |
+ - ! ~ ++ - - (type)* & sizeof() | Right to left |
* / % | Left to right |
<< >> | Left to right |
== != | Left to right |
^ | | Left to right |
&& || | Left to right |
?: | Right to left |
= += -= *= /= %=>>= <<= &= ^= |= | Right to left |
, (comma) | Left to right |
Similar Reads
Properties in Objective-C
The object-oriented programming language Objective-C is largely used to create applications for Apple's macOS and iOS platforms. It incorporates all of the characteristics of C while also providing more features for object-oriented programming, making it a superset of the C programming language. The
5 min read
Preprocessors in Objective-C
Preprocessors help the software experts in tasks like Reusability and Conditional compilation. This article focuses on discussing Preprocessors in Objective-C. What is Objective-C?The Objective-C language is a general-purpose, dynamic, and high-level programming language designed to enable object-or
6 min read
Pointers in Objective-C
In Objective-C, Pointers are the variables that hold the memory address of another variable. You must have declared the pointer variable before its use. The size of the pointer depends on the architecture of the system. The pointer variable can be defined as a char, int, float, double, or any other
5 min read
Strings in Objective-C
Strings are a fundamental concept in programming, and they are used to represent text in many different applications. In Objective-C, a string is an object that represents a sequence of characters. A string can be created using several built-in classes and methods, which allow you to manipulate the
4 min read
Structures in Objective-C
Objective-C is an object-oriented programming language that adds small talk-style messaging to the C programming language. Follows a bottom-up programming approach. It incorporates concepts from both procedural and object-oriented programming languages. Objective-C support structures. So in this art
5 min read
Variables in Objective-C
A variable is a place for data storage that our programs can access or manipulate. In Objective-C, variables have a defined type that specifies their shape and layout. They also cover the gamut of values and operations that we are capable of performing. Before moving further, we understand the guide
4 min read
Polymorphism in Objective-C
One of the fundamental concepts in object-oriented programming is polymorphism. It enables objects to take on a variety of forms depending on their context. In Objective-C, polymorphism allows a class to have multiple behaviors depending on the type of data it is acting on. It is one of the key char
4 min read
Pointer to Pointer in Objective-C
Pointers in Objective-C are a powerful and essential concept for any programmer to master. Pointers allow you to manipulate data stored in memory directly and are used to store the address of a variable. Pointer-to-pointer also known as a double pointer, is a type of pointer that holds the address o
4 min read
Data Types in Objective-C
In Objective-C, each variable has an associated data type and each data type has different amounts of memory required in a system. A data type is a classification of data that tells the compiler or interpreter how the programmer intends to use the data. Or in other words, a data type represents what
8 min read
Pointer to Arrays in Objective-C
In Objective-C, a pointer to an array is a way to store multiple values of the same data type in contiguous memory locations. These arrays can be manipulated and accessed using pointers, which are variables that store the memory address of another variable. In Objective-C, pointers to arrays are use
4 min read