Custom Search

Wednesday 31 March 2010

Processing-06. Data Type: Variables-01

Variables

  In the Processing, programers use various kinds of variables in order to assign numbers, texts, or images so that they can be reused many times within a program. Every variable has a specific data type that defines the category of data it can hold to let the computer know how to manage it. int, float, boolean, char, String, etc. are such functions to assign data into different variables.
  Variables should have names that describe their contents easily and concisely. Their names should not be tedius, long, or too cryptic.
e.g.) roomTemp, outTemp, inTemp are the good examples for the names of variables than t, x, y, z.

  However, there are a few absolute rules in naming vairables. They cannot start with numbers, and they must not be a reserved word such as int, size, color, and null. The Processing language is listed in the reference included with the software, or can be referred to the linked .pdf file.

1. Numbers
  There are two types of numeric data used in the Processing: interger and floating-point.
  Intergers are whole numbers such as 10, -10, and 82, and floating-point numbers are numbers with a decimal point such as 8.5 and -10.4. Both of numeric data can be positive, negative, or zero.
  int is the function to define intergers and float the floating-point numbers. Once it is defined, the data type cannot be changed or restated. (an exception is made when it has a different scope. it will be discussed at the latter post.)
e.g.) int x=10;         // define x as an interger with the value of 10;
        float y=10.1;   // define y as a floating-point number with the value of 10.1

An interger must be defined as an interger, and a floating-point number as a floating-point number.
e.g.) int x=10.1;      // 10.1 is a floating-point number and cannot be assigned to x as an interger.

2. True / False
  The simplest data type in the Processing must be a boolean variable. It can have only one of two values; true or false and can be defined with boolean function.
e.g.) boolean b=true;   // define b as a boolean data with the value of 'true'
        println(b);            // display the value of b in the console, which is 'true'.

3. Text
  Other data such as typographic symbols, words, or sentences can also be assigned to variables with the functions such as char and String. According to the length of the text, char defines a variable with one letter using single quotes.
e.g.) char a='i';         // defines a with the value of 'i'.
        char b='ing';     // cannot define b because 'ing' has more than one letter.

String can define variables with words or sentences using double quotes. (The word String must be capitalized since it is an object. It will be discussed later.)
e.g.) String a="S";            // defines a with the value of 'S'
        String b="School";   // defines b with the value of 'School'
        String c="is over";   // defines c with the value of 'is over'
        String d=b+c;         // defines d with the combination of b and c; that is 'School is over'
        String e='out';        // an error occurs because it uses the single quotes.


No comments:

Post a Comment