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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc
15+ min read