C# Increment and Decrement Operators

In this chapter you will learn:

  1. What are C# Increment and Decrement Operators
  2. How to use C# Increment and Decrement Operators
  3. Example for C# Increment and Decrement Operators

Description

The increment and decrement operators (++, --) increment and decrement numeric types by 1.

The operator can either precede or follow the variable which update the variable before or after the expression is evaluated.

Syntax

To place C# Increment and Decrement Operators before the variable.


++variable;
--variable;

To place C# Increment and Decrement Operators after the variable.


variable--;
variable++;

Example

For example:


int x = 0; /*from  w  w  w  .  j ava2  s.co m*/
Console.WriteLine (x++);   // Outputs 0; x is now 1 
Console.WriteLine (++x);   // Outputs 2; x is now 2 
Console.WriteLine (--x);   // Outputs 1; x is now 1 

Next chapter...

What you will learn in the next chapter:

  1. Use % operator to get remainder
  2. Example for C# Remainder operator
  3. How to use remainder operator to determine smallest single-digit factor
Home »
  C# Tutorial »
    C# Language »
      C# Operators
C# Arithmetic Operators
C# Arithmetic Assignment Operator
C# Increment and Decrement Operators
C# Remainder operator
C# Relational operators
C# Conditional Operators
C# Ternary operator(The ? Operator)
C# Bitwise Operators
C# Bitwise Shift Operators
C# sizeof Operator
C# typeof Operator