Friday 17 August 2018

C#: Arithmetic Operators: ++, --

++ Increment operator; increments a value by 1
-- Decrement operator; decrements a value by 1

Pre increment
    Syntax:
    ++variable
      pre increment operator increments the variable value by 1 immediately.
        
Program.cs
using System;

class Program
{
    static void Main(string[] args)
    {
        int var = 10;

        Console.WriteLine("var = {0}", ++var);
    }
}

Output
var = 11

Post increment
    Syntax:
        variable++

    Post increment operator increments the variable value by 1 after executing the current statement.

Program.cs
using System;

class Program
{
    static void Main(string[] args)
    {
        int var = 10;

        Console.WriteLine("var = {0}", var++);
        Console.WriteLine("var = {0}", var);
    }
}


Output
var = 10
var = 11

Pre Decrement
    Syntax:
        --variable

    Pre decrement operator decrements the variable value by 1 immediately.

Program.cs
using System;

class Program
{
    static void Main(string[] args)
    {
        int var = 10;

        Console.WriteLine("var = {0}", --var);
    }
}


Output
var = 9

Post Decrement
    Syntax:
        variable--

Post decrement operator decrements the variable value by 1 after executing the current statement.

Program.cs
using System;

class Program
{
    static void Main(string[] args)
    {
        int var = 10;

        Console.WriteLine("var = {0}", var--);
        Console.WriteLine("var = {0}", var);
    }
}


Output
var = 10
var = 9





Previous                                                 Next                                                 Home

No comments:

Post a Comment