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

Toán tử tăng (++) và giảm (-) trong C # là gì?

Các toán tử gia tăng

Để tăng một giá trị trong C #, bạn có thể sử dụng các toán tử gia tăng, tức là Các toán tử tăng trước và sau tăng.

Sau đây là một ví dụ -

Ví dụ

using System;

class Demo {
   static void Main() {
      int a = 250;
      Console.WriteLine(a);

      a++;
      Console.WriteLine(a);

      ++a;
      Console.WriteLine(a);

      int b = 0;
      b = a++;
      Console.WriteLine(b);
      Console.WriteLine(a);

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

Toán tử giảm dần

Để giảm một giá trị trong C #, bạn có thể sử dụng các toán tử giảm dần, tức là Các toán tử giảm trước và sau giảm dần.

Sau đây là một ví dụ -

Ví dụ

using System;

class Demo {
   static void Main() {
      int a = 250;
      Console.WriteLine(a);

      a--;
      Console.WriteLine(a);

      --a;
      Console.WriteLine(a);

      int b = 0;
      b = a--;
      Console.WriteLine(b);
      Console.WriteLine(a);

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