Technology Programming

How to Multiply in Visual Basic

    • 1). Define a variable to hold the result of the multiplication operation. In Visual Basic, this is done with a Dim statement. To declare an integer use:

      Dim thisResult As Integer

    • 2). Use the multiplication operator "*" with two integers and assign the result to the variable with the statement:

      thisResult = 6 * 2

      In this instance, the variable thisResult is assigned the value of 12.

    • 3). Include parentheses in any complex arithmetic statement to specify the order in which the operation is to be performed. Due to operator precedence rules, these two statements produce different results:

      thisResult = (2+4) * (1+3)

      thisResult = 2+4 * 1 + 3

      In the first example, the variable thisResult is assigned a value of 24 since the operators inside the parentheses are evaluated before the multiplication operator. In the second example, thisResult is assigned a value of 9. Visual Basic evaluates multiplication and division before addition or subtraction.



Leave a reply