Assigning a value to a variable

Variables are useless until you store data in them. When you store data in a variable, it’s called assigning a value to a variable.

Warning! As soon as you create a variable, it’s a good idea to assign an initial value to that variable, such as 0 or an empty string like “”. In some programming languages, declaring a variable automatically fills that variable with random data. If you forget to clear out a variable by storing an initial value and you then try to use that variable in a calculation, the random data automatically stored in a variable could crash your program.

The simplest way to assign a value to a variable is to use the equal sign. This method enables you to assign a number or string to a variable, as in the following Liberty BASIC example:

 


CEOSalary = 9000000
Message2Workers$ = “So I make more than I’m worth.”

In the first line, you assign the number 9000000 to a variable by the name of CEOSalary. In the second line, you assign the string So I make more than I’m worth. to the variable Message2Workers$.

Technical Stuff If you assign a string to a variable in Liberty BASIC, the variable name must include the dollar sign ($) at the end. If you assign a number to a variable, don’t include a dollar sign in the variable name. In other dialects of BASIC, such as REALbasic, you don’t have to use the dollar sign at the end of a variable that contains strings. Like spoken languages, programming languages consist of minor variations (dialects) that force you to do something that another version of that same language may ignore completely.

To use the preceding Liberty BASIC code in REALbasic, you first have to declare your two variables before assigning a value to them, as shown here:

 


Dim CEOSalary As Integer
Dim Message2Workers As String
CEOSalary = 9000000
Message2Workers = “So I make more than I’m worth.”

Similarly, you must declare your variables first in C++, as in this example:

 


#include <iostream.h>
#include <stdio.h>
using namespace std;
int main()
{
   int CEOSalary;
   string Message2Workers;
   CEOSalary = 9000000;
   Message2Workers = “So I make more than I’m worth.”;
   cout << “nPress ENTER to continue…” << endl;
   getchar();
   return 0;
}

Technical Stuff The C++ language doesn’t include a string data type, so you have to add the using namespace std; command near the top of your program. This provides commands that define a string data type that you can use in your C++ program.

Tip Using variables in C++ is usually a two-step process. First, you must declare a variable name and its data type. Second, you have to assign a value to the variable. To save time, C++ lets you declare a variable and assign a value to it all in a single line such as

 


int CEOSalary = 9000000;

This C++ code tells the computer, “Create a variable that can only hold integer values (int), name the variable CEOSalary, and store the number 9000000 in it.”

Remember In most programming languages, you assign a value to a variable by using the equal sign (=). However, in Revolution, you assign a value to a variable by using the into command, as shown here:

 


put 9000000 into CEOSalary
put “So I make more than I’m worth.” into Message2Workers

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

Leave a Reply



eXTReMe Tracker