This content originally appeared on DEV Community and was authored by islomAli99
Assalamu aleykum hurmatli dasturchilar, bugun sizlar bilan C# dasturlash tilida Arefmetik operatsiyalar haqida to'liq gaplashamiz.
C# tilida arifmetik amallar bir operand (unary), ikki operand (binary), va uch operand (ternary) ustida bajariladi. Keling, barchasini ko'rib chiqamiz.
Binary Operations (Ikki operandli amallar)
+
- Ikki sonni qo'shish
int x = 10;
int z = x + 12; // 22
-
- Ikki sonni ayirish
int x = 10;
int z = x - 6; // 4
*
- Ikki sonni ko'paytirish
int x = 10;
int z = x * 5; // 50
/
- Ikki sonni bo'lish
int x = 10;
int z = x / 5; // 2
double a = 10;
double b = a / 3; // 3.3333333333333335
Agar ikkala operand butun son bo'lsa, natija ham butun son bo'ladi va u yaxlitlanadi.
double x = 10 / 4; // 2
Ushbu muammodan chiqish uchun operandlarni double yoki float sifatida aniqlash kerak:
double x = 10.0 / 4.0; // 2.5
%
- Ikki sonning bo'lishdan qolgan qoldig'ini olish.
double x = 10.0 % 4.0; // 2
Unary Operations (Bir operandli amallar)
++
- Inkrement (o'sish)
Prefiks ⇒ ++x
- avval x qiymati 1 ga oshiriladi, keyin natija qaytariladi.
Postfiks ⇒ x++
- avval x qiymati qaytariladi, keyin 1 ga oshiriladi.
int x = 5;
int y = ++x;
Console.WriteLine($"{x} - {y}"); // x = 6, y = 6
int n = 5;
int m = n++;
Console.WriteLine($"{n} - {m}"); // n = 6, m = 5
--
- Dekrement (kamayish)
Dekrement ham prefiks --x
va postfiks x--
shakllarida mavjud.
Amallarni bajarish tartibi
Inkrement (++)
, dekrement (--)
Ko'paytirish (*)
, bo'lish (/)
, qoldiq olish (%)
Qo'shish (+)
, ayirish (-)
int a = 3;
int b = 5;
int c = 40;
int d = c-- - b * a;
Console.WriteLine($"a={a}; b={b}; c={c}; d={d}"); // a = 3; b = 5; c = 39; d = 25
This content originally appeared on DEV Community and was authored by islomAli99
islomAli99 | Sciencx (2024-07-31T19:49:43+00:00) C# da Arefmetik operators. Retrieved from https://www.scien.cx/2024/07/31/c-da-arefmetik-operators/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.