We need to be able to work with data in our programs. You can create variables as placeholders for values that will change as the program runs, and you can create constants to name values that will be fixed throughout the code.
When you create a variable or constant, you have to indicate the type of data that it will store as well as give it a unique name within the code where it is declared.
To get started, there are three built-in types you can use:
string - when you want to store text data like “Hello” or “7”.
int - to store whole numbers like 7 or -25.
double - to store real numbers like 3.1415, or -0.0017.
Example
This code contains a number of variables: userInput, width, height, and delaySeconds. The code also contains a single constant CIRCLE_RADIUS.
Activities
How would you declare each of these?
A variable to store the user’s name.
A constant for PI with the value 3.1415.
An integer variable for a cost per unit.
A variable to store a phone number.
A constant to store the maximum number of lines, with the value 100.
Answers
1: string name;
2: const double PI = 3.1415;
3: int costPerUnit;
4: string phoneNumber; While this is a "number" it is actually best handed as text. Consider area code etc. Do you ever need to multiply a phone number?