Section 3.5. Adding Integers


3.5. Adding Integers

Our 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.

  1  ' Fig. 3.16: Addition.vb  2  ' Addition program.  3  4  Module Addition  5  6     Sub Main()  7  8        ' variables used in the addition calculation  9        Dim number1 As Integer                       10        Dim number2 As Integer                       11        Dim total As Integer                         12 13        ' prompt for and read the first number from the user 14        Console.Write("Please enter the first integer:")     15        number1 = Console.ReadLine()                         16 17        ' prompt for and read the second number from the user 18        Console.Write("Please enter the second integer: ") 19        number2 = Console.ReadLine() 20 21        total = number1 + number2 ' add the numbers 22 23        Console.WriteLine("The sum is " & total) ' display the sum 24 25     End Sub ' Main 26 27  End Module ' Addition 

 Please enter the first integer: 45 Please enter the second integer: 72 The sum is 117 



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.

Figure 3.17. Primitive types in Visual Basic.

Primitive Types

Boolean

Byte

Char

Date

Decimal

Double

Integer

Long

SByte

Short

Single

String

UInteger

ULong

UShort


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

A common convention (and the one used in this book) is to have the first word in a variable-name identifier begin with a lowercase letter. Every word in the name after the first word should begin with a uppercase letter. For example, identifier firstNumber has a capital N beginning its second word, Number. We use a similar convention for module names, except that the first letter of the first word is also capitalized. Although identifiers are not case sensitive, using these conventions helps make your programs more readable. In this book, we use widely adopted Visual Basic naming conventions.


Good Programming Practice 3.3

Declaring each variable on a separate line allows for easy insertion of an end-of-line comment next to each declaration. We will follow this convention.


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

Place spaces on either side of a binary operator to make the operator stand out and improve the readability of the statement.


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

Follow a method's End Sub with an end-of-line comment containing the name of the method that the End Sub terminates.




Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net