Numeric expressions using variables

This sample program introduces numeric variables.

The first three instructions construct three variables: intA, intB, and intC:

Note that the initial value of each is 0 by default.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   ' Declare the data type for intA, intB, and intC
     Dim intA As Integer
     Dim intB As Integer
     Dim intC As Integer

   ' Display initial values of the three variables
     lblA.text = CStr(intA)
     lblB.text = CStr(intB)
     lblC.text = CStr(intC)

   ' Assign values to the variables intA, intB and intC
     intA = 3
     intB = intA + 3
     intC = intB + 3

   ' Display the new values of the three variables
     Label1.Text = CStr(intA)
     Label2.Text = CStr(intB)
     Label3.Text = CStr(intC)

   ' Evaluate expressions and display results
     Label4.Text = CStr(intA + intB + intC)
     Label5.Text = CStr(intA * intB * intC)
     Label6.Text = CStr(intA * (intB + intC))
     Label7.Text = CStr(intC / (intA + intB))
     Label8.Text = CStr(2 * (intB - intA))
     Label9.Text = CStr(2 * (intA - intB))
     Label10.Text = CStr(intA ^ 2 + intB ^ 2)
     Label11.Text = CStr(intC / (intA + intB) * 2 * intC / (intA + intB))
     Label12.Text = CStr(intA + intA + intA + intA + intA)
End Sub

After creating the three variables, the program displays their values. They are zero at first.

The next section of the program changes then displays the values of the three variables using assignment statements with the format:

    <Variable name> = <expression>
When the program executes, the expression to the right of the equal sign is evaluated, then that value is assigned to the variable named on the left. It is important to recall that the computer executes the statements in the event handler sequentially. The first one is completed before the second executes, etc. Mentally tracing the sequential execution of the statements will help you debug your programs.

The final portion of the program uses assignment statements to display the values of nine numeric expressions. Be sure you understand each of those statements.


Disclaimer: The views and opinions expressed on unofficial pages of California State University, Dominguez Hills faculty, staff or students are strictly those of the page authors. The content of these pages has not been reviewed or approved by California State University, Dominguez Hills.