UCR CS 5 - Chapter3 Operations and Storing the Results

Unformatted text preview:

Chapter 3 Performing Operations and Storing the Results 3 1 Variables Visual Basic NET allows you to store values in a variable Creating a variable requires the same specifications as creating an object You must allocate the appropriate amount of space and associate a name and data type for it To reference the stored value throughout the program you must give variable a valid variable name You must select from a list of variable data types indicating to Visual Basic NET how much space to allocate and how to process and display the variable 1 The Visual Basic NET Coach Chapter 3 Performing Operations and Storing the Results Variable Data Types Variables of different data types exist so that you can store different types of values Integer is one of the data types available in Visual Studio NET to represent whole numbers Typically you would represent integers are a number following the pattern 3 2 1 0 1 2 3 Positive negative numbers and the number 0 are all numbers included 2 The Visual Basic NET Coach Chapter 3 Performing Operations and Storing the Results Selecting the Proper Data Type Visual Basic NET provides three data types that store integers Which data type is chosen depends on the range of values required by the variable Maximum and minimum size of a variable must be taken into account Short data type is for values between 32 768 and 32 767 Integer data type can store a value from 2 147 483 648 to 2 147 483 647 Long can store a value from 9 223 372 036 854 775 808 to 9 223 372 036 854 775 807 3 The Visual Basic NET Coach Chapter 3 Performing Operations and Storing the Results Selecting the Proper Data Type Continued You can use the Long data type in all cases to safeguard against choosing the wrong data type However this will waste memory since Long variable takes twice the space of an Integer variable If you choose a variable to be a Short and then set it to a value out of the range of the variable you will get an execution error a run time error Overflow is the term used to describe when you try to store a value in a variable that is too big If you run the following code you will get an execution error like shown below Private Sub btnCalculate Click Dim shtVariable As Short shtVariable 32767 shtVariable shtVariable 1 End Sub 4 The Visual Basic NET Coach Chapter 3 Performing Operations and Storing the Results Other Variable Data Types Visual Basic NET provides multiple options for selecting the data type of the variable to use when storing decimal numbers Each data type for numerical values has different precisions and storage requirements You can select from Single Double or Decimal when creating a decimal variable listed in increasing order of precision and storage requirements Visual Basic NET provides the String data type to allow the storage of characters You do not need to define how much space is required for Strings because a String s storage requirement is directly related to the length of the string that you wish to store A String is specified as a double quote a series of characters and another double quote The Date data type allows you to store a date You need to enclose the date in signs to use this data type 5 The Visual Basic NET Coach Chapter 3 Performing Operations and Storing the Results Data Types Summary Data Type Description Range Boolean Logical data True or False Date Date and time data January 0100 to December 31 9999 Decimal Large floating point number Varies in size depending on the value stored can hold values much larger or more precise than a Double Double Large or high precision floating point numbers Floating point number with up to 14 digits of accuracy Integer Large integer number 2 147 483 648 to 2 147 483 647 Long Really large integer numbers 9 223 372 036 854 775 808 to 9 223 372 036 854 775 807 Short Small integer numbers 32 768 to 32 767 Single Small floating point numbers Floating point number with up to 6 digits of accuracy String Character data Varies based on the number of characters 6 The Visual Basic NET Coach Chapter 3 Performing Operations and Storing the Results Drill 3 1 In each real world situation that follows list the variable data type that would be most appropriate 1 A variable to store an hourly wage of an employee Answer Decimal 2 A variable to store the average score on an exam that has the lowest possible grade of 0 and highest grade of 100 Answer Short 3 A variable to store the sum of 50 test scores on an exam that has the lowest possible grade of 0 and highest grade of 100 Answer Short 4 A variable to store the sum of 500 test scores on an exam that has the lowest possible grade of 0 and highest grade of 100 Answer Integer 7 The Visual Basic NET Coach Chapter 3 Performing Operations and Storing the Results Drill 3 1 Continued In each real world situation that follows list the variable data type that would be most appropriate 5 A variable to store the sum of 5 000 test scores on an exam that has the lowest possible grade of 0 and highest grade of 100 Answer Integer 6 A variable to store the total number of products ordered Up to 1 billion orders can be placed and each order can contain up to three products Answer Long 7 A variable to store the expression The 76ers are looking great this year Answer String 8 A variable to store tomorrow s date Answer Date 8 The Visual Basic NET Coach Chapter 3 Performing Operations and Storing the Results Drill 3 2 If the following code were executed would an overflow occur If so why Private Sub btnCalculate Click Dim shtVariable As Integer shtVariable 32768 shtVariable shtVariable 1 End Sub Answer An overflow will not occur 9 The Visual Basic NET Coach Chapter 3 Performing Operations and Storing the Results Drill 3 3 If the following code were executed would an overflow occur If so why Private Sub btnCalculate Click Dim shtVariable As Integer shtVariable 10000 shtVariable shtVariable 3 End Sub Answer An overflow will not occur 10 The Visual Basic NET Coach Chapter 3 Performing Operations and Storing the Results Drill 3 4 If the following code were executed would an overflow occur If so why Private Sub btnCalculate Click Dim shtVariable As Integer shtVariable shtVariable shtVariable shtVariable End Sub 32767 shtVariable 5 shtVariable 5 shtVariable 1 Answer An overflow will occur on the last assignment of shtVariable 11 The Visual Basic NET Coach Chapter 3 Performing Operations and Storing the Results Variable Names A variable name in Visual Basic NET begins with a letter and may be


View Full Document

UCR CS 5 - Chapter3 Operations and Storing the Results

Download Chapter3 Operations and Storing the Results
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter3 Operations and Storing the Results and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter3 Operations and Storing the Results 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?