Operators in Objective-C are symbols that perform specific operations on one or more operands. They are essential for manipulating data and controlling program flow. Understanding these operators is crucial for effective Objective-C programming.
Arithmetic operators perform mathematical calculations on numeric operands.
These operators compare two values and return a Boolean result.
Logical operators are used to combine conditional statements.
These operators assign values to variables.
int a = 10;
int b = 3;
int sum = a + b; // 13
int difference = a - b; // 7
int product = a * b; // 30
int quotient = a / b; // 3
int remainder = a % b; // 1
int x = 5;
int y = 10;
BOOL isEqual = (x == y); // NO
BOOL isNotEqual = (x != y); // YES
BOOL isGreater = (x > y); // NO
BOOL isLessOrEqual = (x <= y); // YES
BOOL logicalAnd = (x < y) && (y > 0); // YES
BOOL logicalOr = (x > y) || (y > 0); // YES
BOOL logicalNot = !(x == y); // YES
Operator precedence determines the order in which operators are evaluated in an expression. Parentheses can be used to override the default precedence.
Precedence | Operator |
---|---|
1 (Highest) | () [] -> . |
2 | ! ~ ++ -- + - * & |
3 | * / % |
4 | + - |
5 | << >> |
6 | < <= > >= |
7 | == != |
8 | & |
9 | ^ |
10 | | |
11 | && |
12 | || |
13 | ?: |
14 (Lowest) | = += -= *= /= %= &= ^= |= <<= >>= |
Understanding Objective-C Operators is crucial for writing efficient and error-free code. They form the foundation for manipulating Objective-C Data Types and controlling program flow in Objective-C If-Else Statements and Objective-C Loops.