Section 4e

Data Assignment


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Putting Data into Play

The title of this chapter is "Data Abstraction". While you are not quite through with it, you should have a pretty good feeling for what that means now. You use symbols to represent data that will be used in your programs. When you write the program, you will not know the ages, names, addresses, and other information that your users may enter into the program, so you have to provide variables that can hold that data when it comes in. As you will soon see, the variables will be involved with all the action. The processing, the mathematics, and all the work your program conducts centers around the data that the users provide to your program. This means that variables will be at the center of the battle.

However, as you have read previously, your variables can only hold one type of data at a time, a distinction known as strongly typed. You have to decide in advance what kind of data each variable will hold, and you will define that variable with the necessary data type. The data types provided by the programming language are pretty simple, but you will be able to work with many other kinds of data as you progress in the course, and continue to learn about the tools that are available.

As mentioned earlier in this chapter, one of the simplest ways to think of variables in a program is as buckets. Each bucket can only hold one type of data, however, the data can change or vary, which of course leads to the name variable. So an integer bucket can hold 5, 98, 169, and so on. A double bucket can hold 4.56, 7.89, 2.45, and so on. A character bucket can hold any character, such as 'Z', 'b', 'Q', and so on, and a string bucket can hold "Susan", or "George", or "Melanie". The values in some variables are very likely to change as a program progresses - and again, as you should understand by now, that's why they call them variables.

You have obviously used variables in your Algebra and other math classes previously. Programming with variables is a natural extension of the use of these variables. Always think of the variables as “buckets” that hold the specified kind of data you need for your program. If it helps, consider the graphic below as a mental model of what is happening with your data:

Buckets with labels holding different kinds of data

The buckets have a data type, meaning that the buckets can only hold certain kinds of data. The bucket will have a label, which is the identifier you gave it when you declared it. And the bucket will always have some data in it. Even if you don’t put data in it yourself, the memory can never be empty; it will have whatever data was left in it the last time the memory space was used, whether by your own program or possibly by other programs running at the same time as yours.

Keep this metaphor in mind as you develop your first few programs. It will help you think about how your data is stored.

Putting Variables to Good Use

The second initial operation with variables is the assignment operation. This is the part where you have to modify your previous learning a little. The "equals sign" or "=" is used to assign values to variables. In your previous mathematics classes, the "equals sign" meant, well, that the things on both sides were equal. Here is an example mathematical equality operation (i.e., equation):

sf = si + vot + ½ at2

Now consider the following mathematical expression:

sf = sf + vot

Given how the format looks like the previous expression, this operation also appears to be an equation. However, it could never be an equation. The sf quantity could equal sf (i.e., itself), but it could never equal itself plus something else. This is clearly not an equation.

Enter the assignment operation. The "equals sign" in programming does not mean "equals", so now that you are introduced to it, it will no longer be called an "equals sign" in this course. From this point forward, the symbol "=" is to be called an assignment operator, because that is what it is actually doing in the programming environment. It is placing whatever value is found from the right side of the assignment operator into the variable on the left side of the assignment operator.

Here are some examples:

myAge = 74; // the variable myAge
// is assigned the value 74

mySalary = 4.95; // the variable mySalary
// is assigned the value 4.95

midInitial = ‘X’; // the variable midInitial
// is assigned the value ‘X’

In each case above, some value is assigned to the variable on the left. You would state, “myAge ‘is assigned’ seventy-four”, others might say “myAge ‘gets’ seventy-four”. It doesn’t matter exactly how you say it as long as you understand that the identifier on the left of the assignment operator represents a data storage location where the final result of all actions and data on the right side of the assignment operator should be placed.

As an example of “actions” on the right side of the assignment operator, if there are any calculations or processes to be conducted on the right side of the assignment operator, they will all be conducted before the assignment operation is implemented. While you may not have interacted with methods yet, you can still understand the format of the following actions:

// find discriminant using three coefficients
// method: calcDiscriminant
discrim = calcDiscriminant( coef_A, coef_B, coef_C );

// find square root of given value
// method: calcSqRoot
sqRootVal = calcSqRoot( value );

All actions on the right side of the assignment operator must be conducted first, and then the result will be assigned to the variable on the left.

You should also be aware that you can declare a variable and assign it at the same time if that is appropriate for your specified task. Here is one way to initially assign data to a variable.

// declare the variable
int myAge;

// assign the variable
myAge = 32;

And here is an easier way.
// declare and assign the variable
int myAge = 32;

Remember that each data type only wants to hold the kind of data it is supposed to hold. If you were to assign a floating point amount to an integer variable, as shown below, the compiler will most likely display an error, or at least a warning, that data loss is likely. For example, if myAge is of type int (i.e., an integer), consider the following assignment operation.

myAge = 4.95;

In this example, since myAge can only hold integer data, the .95 (i.e., the 95/100) part of the data would be lost to a process called truncation (i.e., it would be cut off, not rounded), if this were attempted. On the other hand, if you made the assignment to a double type (i.e., a floating point variable) as shown below, data would not be lost, since a double data type can hold values with or without fractional parts.

 

mySalary = 74;

However, if you are using double data types to store integer data, while you may not lose any data, you are not using the data types properly and this can cause confusion among others who may be trying to evaluate your program code.

As this reference progresses, you will find that assignment operations are easy and natural. However, at first you may experience a few difficulties. Simple as these actions are, you just need to practice with them for a bit and you will get comfortable.

Watch this video to learn more about assignment operations.