3.5. Adding IntegersOur next program (Fig. 3.16) inputs two integers (whole numbers) entered at the keyboard by a user, computes the sum of these integers and displays the result. As the user enters each integer and presses the Enter key, the integer is read into the program and added to the total. Figure 3.16. Addition program that adds two integers entered by the user.
Lines 911 are declarations, which begin with keyword Dim (a contraction of the word "dimension"). The words number1, number2 and total are the names of variables. All variables must be declared before they can be used in a program. The declarations in lines 911 specify that the variables number1, number2 and total are data of type Integer; that is, these variables store integer values. Types already defined in Visual Basic, such as Integer, are known as primitive types. Primitive type names are keywords. The 15 primitive types are listed in Fig. 3.17 and discussed further in Chapter 7. Recall that keywords cannot be used as identifiers.
A variable name can be any valid identifier. Variables of the same type can be declared in separate statements or they can be declared in one statement with each variable in the declaration separated by a comma. The latter format uses a comma-separated list of variable names. Good Programming Practice 3.2
Good Programming Practice 3.3
Line 14 prompts the user to enter the first of two integers that will be summed. Line 15 obtains the value entered by the user and assigns it to variable number1. The method ReadLine (line 15) causes the program to pause and wait for user input. After entering the integer via the keyboard, the user presses the Enter key to send the integer to the program. Once the user has entered a number and pressed Enter, the number is assigned to variable number1 (line 15) by an assignment, =. Lines 1819 prompt the user to enter a second integer and assign the entered value to number2. Good Programming Practice 3.4
Technically, the user can send any character to the program as input. For this program, if the user types a non-integer value, such as "hello," a run-time error occurs (Fig. 3.18). Chapter 12, Exception Handling, discusses how to handle such errors to make programs more robustthat is, able to handle run-time errors and continue executing. Figure 3.18. Dialog displaying a run-time error.
The assignment statement in line 21 (Fig. 3.16) calculates the sum of the Integer variables number1 and number2 and assigns the result to variable total. Line 23 displays the total of the two values. The argument to method WriteLine "The sum is " & total uses the string concatenation operator, &, to combine the string literal "The sum is" and the value of the variable total (the Integer variable containing the sum calculated in line 21). The string concatenation operator combines two strings (i.e., to join them together). This operation results in a new, longer string. If an operand given to the string concatenation operator is a number (e.g., an integer), the program automatically creates a string representation of the number. When reading or writing a program, you may find it difficult to match End Sub statements with their method declarations. For this reason, you may want to include an end-of-line comment after End Sub (indicating which method is ending), as we do in line 25. This practice is especially helpful when modules contain multiple methods. Good Programming Practice 3.5
|