Increment And Decrement Operators Using C# Code

Introduction

In this blog, we will write a C# program for the incremental and decremental mathematical operators.

Increment Operator

The increment operator increases a value by one and it's just simply two plus operators together. If the incremental operators is used before the operand it returns the value after the increase .
If int a equals five then plus plus a returns six.
Now if the incremental operator is used after the operand it actually returns the value before the increase. So if int a equals five a plus plus returns five. This can sound very confusing but understand that both a plus plus a would both return six on the line after the increase. This will make a lot of sense once we see a demonstration of the incremental operand.
  • ++ : Increment Operator - Increases value by 1
  • if used before the operand it returns the value after the increase
  • int a = 5;++ a returns 6.
  • If used after the operand it returns the value before the increase
  • int a = 5 ; a++ returns 5.
  • Both a++ and ++a return 6 on the line after the incease

Decrement Operator

The decrement operator subtracts the value by one and it's simply two subtraction operators together, much like the incremental operator if the decrement operator is used before the operand it returns the value after the decrease.
If int a equals 5 then minus minus a returns four. It the decrement operator is used after the operand then it returns the value before the decrease so if int a equal 5 a minus minus also returns 5 but once again both a minus minus and minus minus a return 4 on the line after the decrease now
  • -- : Decrement Operator - Decreases value by 1
  • if used before the operand it returns the value after the decrease
  • int a = 5;-- a returns 4.
  • If used after the operand it returns the value before the decrease
  • int a = 5 ; a-- returns 5.
  • Both a-- and --a return 64on the line after the decrease

Increment and Decrement Operators in C# Code

This code sample shows you how to do it in C# Code.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace ConsoleApplication1 {
  7. class Program {
  8. static void Main(string[] args) {
  9. int a = 10;
  10. // Incremental Operator
  11. Console.WriteLine("Incremental Operator");
  12. Console.WriteLine();
  13. Console.WriteLine(++a);
  14. Console.WriteLine(a);
  15. Console.WriteLine();
  16. Console.WriteLine(a++);
  17. Console.WriteLine(a);
  18. Console.WriteLine();
  19. a++;
  20. Console.WriteLine(a);
  21. ++a;
  22. Console.WriteLine(a);
  23. Console.WriteLine();
  24. // Decrement Operator
  25. Console.WriteLine("Decremental Operator");
  26. Console.WriteLine();
  27. Console.WriteLine(--a);
  28. Console.WriteLine(a);
  29. Console.WriteLine();
  30. Console.WriteLine(a--);
  31. Console.WriteLine(a);
  32. Console.WriteLine();
  33. a++;
  34. Console.WriteLine(a);
  35. ++a;
  36. Console.WriteLine(a);
  37. Console.WriteLine();
  38. }
  39. }
  40. }
Save the program.
Now run the increment and decrement operator code.
Click on start button and debug the program
Increment and Decrement Operators using C# code
Output to the Increment and dercrement operator code.
Increment and Decrement Operators using C# code
I hope you understood how to do the increment and dercrement operator code using C# Code.
Ebook Download
View all
Learn
View all