عملگرها
عملگرها در کوروش مشابه C کار میکنند. آنها بر اساس функциональность و اولویت دستهبندی میشوند. عملگرها بر اساس اولویت (priority) و انجمنیبودن (associativity) (چپبهراست یا راستبهچپ) ارزیابی میشوند.
عملگرهای حسابی
عملگرهای حسابی محاسبات ریاضی را بین عملوندهای عددی انجام میدهند.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | Addition | 5 + 2 | 7 |
| - | Subtraction | 5 - 2 | 3 |
| * | Multiplication | 5 * 2 | 10 |
| / | Division | 5 / 2 | 2 |
| % | Modulo (Truncated) | 5 % 2 | 1 |
| | | Bitwise OR (integers) | 6 | 3 | 7 |
| ~ | Bitwise XOR (integers) | 6 ~ 3 | 5 |
| & | Bitwise AND (integers) | 6 & 3 | 2 |
&~ | Bitwise AND-NOT (integers) | 6 &~ 3 | 4 |
| << | Left Shift (integer << non-negative integer) | 3 << 2 | 12 |
| >> | Right Shift (integer >> non-negative integer) | 12 >> 2 | 3 |
عملگرهای مقایسهای
عملگرهای مقایسهای رابطه بین دو عملوند را ارزیابی کرده و یک نتیجه bool (true یا false) برمیگردانند.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | true |
| != | Not equal to | 5 != 3 | true |
| < | Less than | 3 < 5 | true |
| > | Greater than | 7 > 2 | true |
| <= | Less than or equal to | 3 <= 3 | true |
| >= | Greater than or equal to | 5 >= 2 | true |
عملگرهای منطقی
عملگرهای منطقی برای ترکیب یا معکوس کردن مقادیر بولی استفاده میشوند. آنها همواره روی مقادیر true و false عمل کرده و یک نتیجه بولی برمیگردانند.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| && | Logical AND | true && false | false |
| || | Logical OR | true || false | true |
| ! | Logical NOT | !true | false |
عملگرهای انتساب
عملگرهای انتساب برای تخصیص مقادیر به متغیرها استفاده میشوند. سادهترین شکل = است، اما انتسابهای ترکیبی نیز وجود دارند که یک عملیات و انتساب را در یک مرحله انجام میدهند.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| = | Assignment | a = 10 | a = 10 |
| += | Add and assign | a += 5 | a = a + 5 |
| -= | Subtract and assign | a -= 5 | a = a - 5 |
| *= | Multiply and assign | a *= 5 | a = a * 5 |
| /= | Divide and assign | a /= 5 | a = a / 5 |
| %= | Modulo and assign | a %= 5 | a = a % 5 |
| &= | Bitwise AND and assign | a &= 5 | a = a & 5 |
| ~= | Bitwise XOR and assign | a ~= 5 | a = a ~ 5 |
| &~= | Bitwise AND NOT and assign | a &~= 5 | a = a &~ 5 |
| <<= | Left shift and assign | a <<= 2 | a = a << 2 |
| >>= | Right shift and assign | a >>= 2 | a = a >> 2 |
اولویت و انجمنیبودن عملگرها
انجمنیبودن تعیین میکند که عملگرهای با اولویت یکسان چگونه گروهبندی میشوند: چپبهراست یا راستبهچپ.
| Operator / Type | Description | Precedence / Associativity |
|---|---|---|
| my_function(x) | Function call. Evaluates arguments and calls the function. | Very High / Left-to-right |
| array[index] | Array indexing. Access the element at a specific position. | Very High / Left-to-right |
| Type conversion (cast) | Explicitly converts a value to a different type. | High / Right-to-left |
| . | Member access. Accesses a field of a struct or object. | High / Left-to-right |
| -> | Pointer member access. Dereferences a pointer to access a field of a struct or object. | High / Left-to-right |
| *, /, % | Multiplication, division, and modulo operators. | High / Left-to-right |
| &, |, ~, &~ | Bitwise AND, OR, NOT, and custom combination (~&). | High / Left-to-right |
| -X, !X | Unary prefix operators: negation and logical NOT. | High / Right-to-left |
| sizeof | Returns the size of a type or expression in bytes. | High / Right-to-left |
| * | Dereference operator. Accesses the value pointed to by a pointer. | Higher than member access / Right-to-left |
| & | Address-of operator. Returns the memory address of its operand. | Lower than member access / Right-to-left |
| +, - | Addition and subtraction operators. | Mid-High / Left-to-right |
| ==, != | Equality and inequality comparison operators. | Low-Mid / Left-to-right |
| >, <, >=, <= | Relational operators to compare values. | Mid / Left-to-right |
| || | Logical OR. Returns true if at least one operand is true. | Low / Left-to-right |
| && | Logical AND. Returns true only if both operands are true. | Low / Left-to-right |
| Lowest | Represents the lowest precedence. Used internally as a default when no other precedence applies. | Lowest |
عملگرهای یکانی
عملگرهای یکانی روی یک عملوند واحد عمل کرده و مقدار آن را به میزان ۱ تغییر میدهند.
پیشوندی (++i, --i) متغیر را قبل از استفاده در عبارت بهروزرسانی میکند.
پسوندی (i++, i--) متغیر را بعد از استفاده در عبارت بهروزرسانی میکند.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| i++ | Post-increment: returns current value, then increases by 1 | i = 5; j = i++ | i = 6, j = 5 |
| ++i | Pre-increment: increases by 1, then returns the new value | i = 5; j = ++i | i = 6, j = 6 |
| i-- | Post-decrement: returns current value, then decreases by 1 | i = 5; j = i-- | i = 4, j = 5 |
| --i | Pre-decrement: decreases by 1, then returns the new value | i = 5; j = --i | i = 4, j = 4 |
- فقط روی lvalueهای تغییرپذیر کار میکند؛ استفاده روی ثابتها یا rvalueها خطای کامپایل محسوب میشود.
- از تکیه بر افزایشهای چندگانه یک متغیر در یک عبارت واحد خودداری کنید.
عملگر ادغام تهی (Null-Coalescing)
عملگر ادغام تهی || با اشارهگرها برای انتخاب اولین مقدار غیرتهی از زنجیرهای از عملوندها استفاده میشود.
این عملگر عملوندها را از چپ به راست ارزیابی کرده و اولین اشارهگری را که null نیست برمیگرداند.
اگر همه عملوندها null باشند، نتیجه null است.
var p1: int* = null;
var p2: int* = &x;
var p3: int* = &y;
var result1 = p1 || p2; // returns p2
var result2 = p1 || null; // returns null
var result3 = p1 || p2 || p3; // returns p2 (first non-null)

