Custom Search

Wednesday 17 March 2010

Processing-04. Basic Code Elements

Basic Syntax

 

1. Comment( // )
  Comments offer notes or explanation of program coded by you or others. It is difficult to read and understand codes of more than hundreds lines just by looking at them. So, comments help users to understand functions and structures of programs acting like a footnote.
Two forward slashes are used to denote a comment in one line.

e.g.) // You can write a comment here like this.
       // If you want to write more than one line, put another two slashes before writing more.


  If you want to have a comment that is more than one line, you may use the syntax, Multiline Comment( /* */ ).

e.g.) /*
           A forward slash followed by an asterisk allows 
           the comment to continue until the opposite.
           You can write as many as you want here.
        */

2. Function
  Functions allow you to do the basic tasks like drawing shapes, setting colors, calculating numbers, etc. The name of functions usually starts with a lowercase word followed by parameters within parentheses. Functions named with two words use the lower case letter for the first word, and uppercase letter for the later one. e.g.) noFill, strokeWeight, strokeCap, etc.)

e.g.) size(200, 200);      - defines a windows with 200 pixels length and 200 pixels width.
        background(255);   - defines the background color to white(255).

3.  Statement Terminator( ; )
  It acts like a period of a sentence. Each time you finish coding one line, you need to put semicolon at the end of the line. If you do not, it will result in an error message, and the program will not run.
(except when you use this parentheses - { } )


4. Parameter
   It is a variable within the parenthesis at the end of a function that defines color, width, length, size, opacity, etc.


5. Console
  When a program runs, the computer performs it at a rate too fast so that users can not understand which codes are being processed. So, when coding a program, it can be useful to put print() or println() function to display data in the console of PDE(Processing Development Environment) while a program is running.

e.g.) size(300, 300);
        print("Define the size of window");
        background(0);
        print("Define the background of color)";

  This example displays the explanation in the console which process (defining size and background color) is currently being performed using print() function.
  Place the desired texts in quotes to print them to the screen and variables like x, y, and z in the parenthesis to print the value of the variable.
  print() displays continuously the text in one line and println() does in the next line. 

e.g.)  print("10");
         print("10");
result is 1010

         println("10");
         println("10");
result is  10
              10

The "+" operator can be used for combining multiple text elements in one line.

e.g.) int x=10
        println("x" + "=" + x)
result is x=10



No comments:

Post a Comment