Posts

Showing posts with the label C# Increment and Decrement Operators

C# Increment and Decrement Operators

C# Increment and Decrement Operators In C#, the increment (++) and decrement (--) operators are used to increase or decrease the value of a numeric variable by 1. They can be used in both prefix and postfix modes. Prefix Mode:  * Increment: ++x    * Increments the value of x by 1, then returns the new value.  * Decrement: --x    * Decrements the value of x by 1, then returns the new value. Postfix Mode:  * Increment: x++    * Returns the current value of x, then increments it by 1.  * Decrement: x--    * Returns the current value of x, then decrements it by 1. Example: int x = 5; // Prefix increment int y = ++x; // x becomes 6, y becomes 6 // Postfix increment int z = x++; // z becomes 6, x becomes 7 // Prefix decrement int a = --x; // x becomes 6, a becomes 6 // Postfix decrement int b = x--; // b becomes 6, x becomes 5 Console.WriteLine("x: " + x); Console.WriteLine("y: " + y); Console.WriteLine("z: " + z); Console.WriteLine(...