Chapter 11. Using Variables and Performing Calculations

Variables hold values in memory so a program can manipulate them. Different kinds of variables hold different types of data: numbers, text, pictures, Halo scores, even complex groups of data such as employee records.

In this lesson you learn what variables are and how to use them. You learn how to define variables, put data in them, and use them to perform simple calculations.

WHAT ARE VARIABLES?

Technically speaking, a variable is a named piece of memory that can hold some data of a specific type. For example, a program might allocate 4 bytes of memory to store an integer. You might name this memory "payoffs" so you can easily refer to it in the program's code.

Less technically, you can think of a variable as a named place to put a piece of data. The program's code can use variables to store values and perform calculations. For example, a program might store two values in variables, add the values together, and store the result in a third variable.

DATA TYPES

Every variable has a particular data type that determines the kind of data that it can hold. In general, you cannot place data of one type in a variable of another. For example, if price is a variable that can hold a number in dollars and cents, you cannot put the string "Hello" in it.

If you like, you can think of a variable as an envelope with a name written on the outside that can hold some data, but each type of data requires a differently shaped envelope. Integers need ...

Get Stephens' Visual Basic® Programming 24-Hour Trainer now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.