Saturday, November 5, 2011

Variables and Data Types

Variables in programming have five attributes:

  1. Name: A variable's name is linked to information. A descriptive name makes code easier to understand by other programmer's or even by you in the future.
  2. Type: The kind of information that is linked to the variable name. (See discussion of data types below)
  3. Value: The actual information stored.
  4. Scope: Where in a program a variable can be used. (to be discussed with functions)
  5. Lifetime: When the variable exists during each run of the program. (to be discussed with functions)

Python

factor1 = 3
factor2 = 5
quotient = factor1 * factor2

OR

quotient = 3 *5


C++

int factor1, factor2, quotient;
factor1 = 3;
factor2 = 5;
quotient = factor1 * factor2;

OR

int factor1 = 3;
int factor2 = 5;
int quotient = factor1 * factor2;

OR

int quotient = 3 * 5;


Java

int factor1, factor2, quotient;
factor1 = 3;
factor2 = 5;
quotient = factor1 * factor2;

OR

int factor1 = 3;
int factor2 = 5;
int quotient = factor1 * factor2;

OR

int quotient = 3 * 5;

  • When doing math with any of the languages you can either define variables for each number and then define another variable, which uses those variables to actually do the calculations, or you can define a variable by doing the calculations immediately.
  • In Python, type declarations are unnecessary. Unfortunately, this makes it impossible to declare a variable before you define it.
  • In both C++ and Java there are a few ways declare and define variables:
    • you can declare all of your variables first and then define them individually before doing the calculations.
    • you can define each variable as you declare it before doing the calculations.
    • or you can define a variable at declaration with the calculations.

Data Types:
since variables don't need to be declared in Python, all example declarations are applicable to only C++ and Java.

Every programming language has built-in data types that allow you to perform various tasks. Primitive data types are found in most languages. These include:

Numbers
  • Integer: Integers are whole numbers without decimal places. They are usually a sufficient size for whatever data manipulation needs to be accomplished, but they can lead to integer overflow in some situations.
    • int twenty = 20;
  • Short (integer): Shorts are the smallest integer variation in most languages. They are used when it is necessary to save memory
    • short two = 2;
  • Long (integer): The long is a longer variation of the integer. They are used when an integer would lead to overflow.
    • long two_hundred = 200L;
  • Float: Floats are decimal numbers that can contain approximately 7 digits. 
    • float decimal_two = 2.0341f;
  • Double: Doubles are a double precision float. They can contain approximately 15 digits (rather than only 7).
    • double pi = 3.14159265;
Other
  • Boolean: Boolean values are either true or false. They can be used to track conditions (whether the condition is true or false).
    • bool contains_letters = 1;
    • 1 = true, 0 = false
  • Character: Characters are just a single character.
    • char a_letter = 'A';
    • characters must be declared in single quotes
There are other data types as well, but they vary by language. For example, in Python strings (a sequence of characters) are an included data type. In Java, the byte (a variation of the integer, which is even smaller than a short) is a primitive data type.

No comments:

Post a Comment