Computer >> Máy Tính >  >> Lập trình >> C#

Các toán tử tăng và giảm trong C #

Toán tử tăng dần làm tăng giá trị số nguyên lên một tức là

int a = 10;
a++;
++a;

Toán tử giảm dần giảm giá trị số nguyên đi một tức là

int a = 20;
a--;
--a;

Sau đây là một ví dụ minh họa toán tử tăng -

Ví dụ

using System;

class Program {
   static void Main() {
      int a, b;

      a = 10;
      Console.WriteLine(++a);
      Console.WriteLine(a++);

      b = a;
      Console.WriteLine(a);
      Console.WriteLine(b);
   }
}

Đầu ra

11
11
12
12

Sau đây là một ví dụ minh họa toán tử giảm -

int a, b;
a = 10;

// displaying decrement operator result
Console.WriteLine(--a);
Console.WriteLine(a--);

b = a;
Console.WriteLine(a);
Console.WriteLine(b);