Home

Incrementing and Decrementing Values

     

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:

Module Exercise

    Public Function Main() As Integer
        Dim Value As Integer

        Value = InputBox("Enter the Value:")

        MsgBox(Value)

        Value = Value + 1

        MsgBox(Value)

        Return 0
    End Function

End Module

This would produce:

Increment
Increment
Increment

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:

Module Exercise

    Public Function Main() As Integer
        Dim Value As Integer

        Value = InputBox("Enter the Value:")

        MsgBox(Value)

        Value = Value - 1

        MsgBox(Value)

        Return 0
    End Function

End Module

This would produce:

Decrementing a Value
Decrementing a Value
Decrementing a Value

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:

Module Exercise

    Public Function Main() As Integer
        Dim Value As Double = 12.75

        MsgBox("Value = " & Value)

        Value = Value + 2.42

        MsgBox("Value = " & Value)

        Return 0
    End Function

End Module

This would produce:

Incrementing a Value
Incrementing a Value

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:

Module Exercise

    Public Function Main() As Integer
        Dim Value As Double = 12.75

        MsgBox("Value = " & Value)

        Value += 2.42

        MsgBox("Value = " & Value)

        Return 0
    End Function

End Module

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:

Module Exercise

    Public Function Main() As Integer
        Dim Value As Double = 12.75

        MsgBox("Value = " & Value)

        Value -= 2.42

        MsgBox("Value = " & Value)

        Return 0
    End Function

End Module

This would produce:

Incrementing a Value
Decrementing a Value
     
 
 
     
 

Home Copyright © 2010-2016, FunctionX