Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

ตัวดำเนินการเลขคณิต (Arithmetic Operators) ใน C# คืออะไร

ตัวดำเนินการคือสัญลักษณ์ที่บอกคอมไพล์อร์ให้ดำเนินการทางคณิตศาสตร์หรือตรรกะเฉพาะเจาะจง

ตารางตัวดำเนินการเลขคณิต

ตัวดำเนินการคำอธิบายตัวอย่าง
+บวกสองตัวถูกดำเนินการA + B = 30
-ลบตัวถูกดำเนินการตัวที่สองจากตัวแรกA - B = -10
*คูณทั้งสองตัวถูกดำเนินการA * B = 200
/หารตัวเศษด้วยตัวส่วนB / A = 2
%Modulus: เศษจากการหารจำนวนเต็มB % A = 0
++Increment: เพิ่มค่าจำนวนเต็มขึ้น 1A++ = 11
--Decrement: ลดค่าจำนวนเต็มลง 1A-- = 9

ตัวอย่าง

using System; 
namespace Demo { 
    class Program { 
        static void Main(string[] args) { 
            int a = 99; 
            int b = 33; 
            int c; 
            c = a + b; Console.WriteLine("Value of c is {0}", c); 
            c = a - b; Console.WriteLine("Value of c is {0}", c); 
            c = a * b; Console.WriteLine("Value of c is {0}", c); 
            c = a / b; Console.WriteLine("Value of c is {0}", c); 
            c = a % b; Console.WriteLine("Value of c is {0}", c); 
            c = a++; Console.WriteLine("Value of c is {0}", c); 
            c = a--; Console.WriteLine("Value of c is {0}", c); 
            Console.ReadLine(); 
        } 
    } 
}

ผลลัพธ์

Value of c is 132 Value of c is 66 Value of c is 3267 Value of c is 3 Value of c is 0 Value of c is 99 Value of c is 100