| Chapter 3- Data Types and Operators | Boolean Expressions Page 2 3 4 |
| The If Statement Page 2 3 4 5 6 7 8 9 | |
| Arithmetic Operations Page 2 3 4 5 6 | Boolean Operators and Nested If Statements Page 2 3 4 5 6 7 |
| Events and Sequential Processing Page 2 3 4 5 | More Examples Page 2 3 4 5 6 7 8 9 10 11 12 |
| Datatypes and Conversions Page 2 3 4 5 6 7 | Using Check Box and Option Controls Page 2 3 4 5 6 7 8 9 10 |
| Variable Declarations - Local and Global Page 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Exercises Page 2 3 4 5 6 7 8 |
| Chapter 4- Selection Statements | Review Questions |
| Introduction | |
3-5: Variable Declarations (continued)
Which Data Type to Choose?
The Integer and Double data types are chosen in this example because they have a suitable range of values and a suitable precision (number of fractional digits).
It is arguable that we should choose Single for the interestRate and capital variables. The range of values is easily large enough, but the Single data type has only about 6 figure accuracy (i.e. 6 fractional digits). It is conceivable, though perhaps unlikely, that an interest rate might be specified with more than 6 fractional digits, in which case choosing Single will potentially cause problems.
Normally the decision as to which data type to use is simpler than this example. When to use String or Date should be obvious! However, a variable that will store integers could be declared as Byte, Short, Integer or Long. The choice is determined by how large you expect the integers to be. If you know they will never be very large you can choose Short, or even Byte, but otherwise you should choose Integer. The Integer type has limits of about 2 billion (positive or negative). It is rare that the Long type is needed, which has limits of about 10 billion billion (positive or negative)!
Similar considerations apply to the choice between Single and Double, although the range is much larger than for integers. In addition the precision of the values is different - about 6 fractional digits for Single and 14 digits for the Double type.