1.1 Simple Programs

Processing is a programming language that extends the Java programming language. It has very many nice features for creating programs that use graphics. Before we get to that we will look at some simpler programs that will just display some text.

There are two commands for displaying text. They are println and print. We will look at the difference between the two shortly. Here is an example of using println:

println("This will print"); 
Whatever text (in Processing as in many languages this is called a string) is put between the set of quotation marks will get printed exactly as typed. So the output from this simple program would be:
This will print

You can combine several println statements together. Each one will produce one line of output. The following example will produce three lines of output:

println("one line");
println("line 2");
println("A third line");
produces

one line
line 2
A third line
The print statement has the same effect as println except that it doesn't go to the next line after printing something. So, if we modify the previous example to use print instead of println like so:

print("one line");
print("line 2");
print("A third line"); 
we would get
one lineline 2A third line

You can combine the two statements as well. Just remember that you go to the next line after you've printed something with println. This program:

println("abc");
print("def");
print("ghi");
println("jkl");
print("mno");
println("pqr");

will have the following output:

abc
defghijkl
mnopqr

One thing to note is that spaces inside quotes will get printed exactly as you type them. Generally, spaces and blank lines elsewhere will be ignored. The output of this program

println("a space then         several spaces");
println           ("the spaces outside the quotes are ignored");
       println(
              "but don't do this as the program becomes very hard to read"
           )            ; 

would be

a space then         several spaces
the spaces outside the quotes are ignored
but don't do this as the program becomes very hard to read

The program should look like this:

println("a space then         several spaces");
println("the spaces outside the quotes are ignored");
println("but don't do this as the program becomes very hard to read"); 

even though both versions produce exactly the same output.

There is one problem with this system. What happens if we want to include quotation marks in our output? We've been using quotation marks to begin and end the strings we've been printing. So if we wanted this output:

She said, "And I quote".

we might try typing this: println("She said, "And I quote".");

It won't work because we now have a string She said, and then more stuff. We need a way of saying that the second and third double quotes do not indicate the beginning or ending of a string but should be printed as characters. The way we do that is by putting a backslash character in front of the double quote. The correct way of doing the previous example is: println("She said, \"And I quote\".");

The backslash doesn't get printed. It is known as an escape character. It indicates that the next character is a special character. So what happens if we want to print a backslash character? We use two backslashes. The first is the escape character and the second is the special character we want (the backslash).

println("What \"will\" this \\do\\?"); 

would produce

What "will" this \do\?

There are a couple of other special characters that Processing has:

\n gives you a newline character (makes you go to the next line).
\t gives a tab character.

Exercise 1.1

  1. What does this program print?
    print("Processing programs");
    println("   can be run");
    println("in a ");
    print("web");
    println("browser");
    print("just");
    println();
    println();
    println("like Java applets"); 
    1. Rewrite the following program using the suggested indentation style.

      print("This still"); print(" works, but "); println("it sure is ugly."); 
    2. What the program in part a) print?
  2. Write a program to skip two lines, print your name, skip two more lines, print your home-form, skip 5 lines and print your age.
  3. Write a program that would produce the following output using 5 print statements.

    She said, "Take your 'findings' to the Department of
    Redundancy Department.  They'll
    know what to do with them".