Home

Logically Incrementing or Decrementing a Value

 

Incrementing a Variable

We are used to counting numbers such as 1, 2, 3, 4, etc. In reality, when counting such numbers, we are simply adding 1 to a number in order to get the next number in the range. The simplest technique of incrementing a value consists of adding 1 to it. After adding 1, the value or the variable is (permanently) modified and the variable would hold the new value. This is illustrated in the following example:

// This program studies value incrementing
using System;

class Exercise
{
	static void Main()
	{
		int Value = 12;

		Console.WriteLine("Techniques of incrementing a value");
		Console.Write("Value = ");
		Console.WriteLine(Value); 

		Value = Value + 1;

		Console.Write("Value = ");
		Console.WriteLine(Value); 
	}
}

This would produce:

Techniques of incrementing a value
Value = 12
Value = 13

C# provides a special operator that takes care of this operation. The operator is called the increment operator and is represented by ++. Instead of writing Value = Value + 1, you can write Value++ and you would get the same result. The above program can be re-written as follows:

// This program studies value incrementing
using System;

class Exercise
{
	static void Main()
	{
		int Value = 12;

		Console.WriteLine("Techniques of incrementing a value");
		Console.Write("Value = ");
		Console.WriteLine(Value); 

		Value++;

		Console.Write("Value = ");
		Console.WriteLine(Value); 
	}
}

The ++ is a unary operator because it operates on only one variable. It is used to modify the value of the variable by adding 1 to it. Every time the Value++ is executed, the compiler takes the previous value of the variable, adds 1 to it, and the variable holds the incremented value:

// This program studies value incrementing
using System;

class Exercise
{
	static void Main()
	{
		int Value = 12;

		Console.WriteLine("Techniques of incrementing a value");

		Value++;
		Console.Write("Value = ");
		Console.WriteLine(Value); 

		Value++;
		Console.Write("Value = ");
		Console.WriteLine(Value);
		
		Value++;
		Console.Write("Value = ");
		Console.WriteLine(Value);
	}
}

This would produce:

Techniques of incrementing a value
Value = 13
Value = 14
Value = 15
 

Pre and Post-Increment

When using the ++ operator, the position of the operator with regard to the variable it is modifying can be significant. To increment the value of the variable before re-using it, you should position the operator on the left of the variable:

// This program studies value incrementing
using System;

class Exercise
{
	static void Main()
	{
		int Value = 12;

		Console.WriteLine("Techniques of incrementing a value");

		Console.Write("Value = ");
		Console.WriteLine(Value); 

		Console.Write("Value = ");
		Console.WriteLine(++Value);
		
		Console.Write("Value = ");
		Console.WriteLine(Value);
	}
}

This would produce:

Techniques of incrementing a value
Value = 12
Value = 13
Value = 13

When writing ++Value, the value of the variable is incremented before being called. On the other hand, if you want to first use a variable, then increment it, in other words, if you want to increment the variable after calling it, position the increment operator on the right side of the variable:

// This program studies value incrementing
using System;

class Exercise
{
	static void Main()
	{
		int Value = 12;

		Console.WriteLine("Techniques of incrementing a value");

		Console.Write("Value = ");
		Console.WriteLine(Value); 

		Console.Write("Value = ");
		Console.WriteLine(Value++);
		
		Console.Write("Value = ");
		Console.WriteLine(Value);
	}
}

This would produce:

Techniques of incrementing a value
Value = 12
Value = 12
Value = 13

Decrementing a Value

When counting numbers backward, such as 8, 7, 6, 5, etc, we are in fact subtracting 1 from a value in order to get the lesser value. This operation is referred to as decrementing a value. This operation works as if a value is decremented by 1, as in Value = Value – 1:

// This program studies value decrementing
using System;

class Exercise
{
	static void Main()
	{
		int Value = 12;

		Console.WriteLine("Techniques of decrementing a value");
		Console.Write("Value = ");
		Console.WriteLine(Value); 

		Value = Value - 1;

		Console.Write("Value = ");
		Console.WriteLine(Value); 
	}
}

This would produce:

Techniques of decrementing a value
Value = 12
Value = 11

As done to increment, C# provides a quicker way of subtracting 1 from a value. This is done using the decrement operator, that is --. To use the decrement operator, type –- on the left or the right side of the variable when this operation is desired. Using the decrement operator, the above program could be written:

// This program studies value decrementing
using System;

class Exercise
{
	static void Main()
	{
		int Value = 12;

		Console.WriteLine("Techniques of decrementing a value");
		Console.Write("Value = ");
		Console.WriteLine(Value); 

		Value--;

		Console.Write("Value = ");
		Console.WriteLine(Value); 
	}
}
 

Pre Decrementing a Value

Once again, the position of the operator can be important. If you want to decrement the variable before calling it, position the decrement operator on the left side of the operand. This is illustrated in the following program:

// This program studies value decrementing
using System;

class Exercise
{
	static void Main()
	{
		int Value = 12;

		Console.WriteLine("Techniques of decrementing a value");
		Console.Write("Value = ");
		Console.WriteLine(Value); 

		Console.Write("Value = ");
		Console.WriteLine(--Value);
		
		Console.Write("Value = ");
		Console.WriteLine(Value);
	}
}

This would produce:

Techniques of decrementing a value
Value = 12
Value = 11
Value = 11

If you plan to decrement a variable only after it has been accessed, position the operator on the right side of the variable. Here is an example:

// This program studies value decrementing
using System;

class Exercise
{
	static void Main()
	{
		int Value = 12;

		Console.WriteLine("Techniques of decrementing a value");
		Console.Write("Value = ");
		Console.WriteLine(Value); 

		Console.Write("Value = ");
		Console.WriteLine(Value--);
		
		Console.Write("Value = ");
		Console.WriteLine(Value);
	}
}

This would produce:

Techniques of decrementing a value
Value = 12
Value = 12
Value = 11

Techniques of Incrementing and Decrementing a Variable

It is not unusual to add or subtract a constant value to or from a variable. All you have to do is to declare another variable that would hold the new value. Here is an example:

// This program studies value incrementing and decrementing
using System;

class Exercise
{
	static void Main()
	{
		double Value = 12.75;
		double NewValue;

		Console.WriteLine("Techniques of incrementing and decrementing a value");
		Console.Write("Value = ");
		Console.WriteLine(Value);

		NewValue = Value + 2.42;
		
		Console.Write("Value = ");
		Console.WriteLine(NewValue); 
	}
}

This would produce:

Techniques of incrementing and decrementing a value
Value = 12.75
Value = 15.17

The above technique requires that you use an extra variable in your application. The advantage is that each value can hold its own value although the value of the second variable depends on whatever would happen to the original or source variable.

Sometimes in your program you will not need to keep the original value of the source variable. You may want to permanently modify the value that a variable is holding. In this case you can perform the addition operation directly on the variable by adding the desired value to the variable. This operation modifies whatever value a variable is holding and does not need an additional variable.

To add a value to a variable and change the value that the variable is holding, you can combine the assignment “=” and the addition “+” operators to produce a new operator as +=

Here is an example:

// This program studies value incrementing and decrementing
using System;

class Exercise
{
	static void Main()
	{
		double Value = 12.75;

		Console.WriteLine("Techniques of incrementing and decrementing a value");
		Console.Write("Value = ");
		Console.WriteLine(Value);

		Value += 2.42;
		
		Console.Write("Value = ");
		Console.WriteLine(Value); 
	}
}

This program produces the same result as the previous. To decrement the value of a variable, instead of the addition, use the subtraction and apply the same technique. In the above program, the variable can have its value decremented by combining the assignment and the subtraction operations on the variable. This is done with the -= operator. Here is an example:

// This program studies value incrementing and decrementing
using System;

class Exercise
{
	static void Main()
	{
		double Value = 12.75;

		Console.WriteLine("Techniques of incrementing and decrementing a value");
		Console.Write("Value = ");
		Console.WriteLine(Value);

		Value -= 2.42;
		
		Console.Write("Value = ");
		Console.WriteLine(Value); 
	}
}

This would produce:

Techniques of incrementing and decrementing a value
Value = 12.75
Value = 10.33

 

 

Previous Copyright © 2006-2016, FunctionX, Inc. Next