CS4ALL Computer Science For All Of Us
  • Home
  • Community
  • Workshop
    • Workshop Info
    • Schedule
    • Location
    • Sponsors
  • Resources
    • Scratch Resources
CS4ALL
  • Home
  • Community
  • Workshop
    • Workshop Info
    • Schedule
    • Location
    • Sponsors
  • Resources
    • Scratch Resources
  • Home
  • Processing
  • Beginner
  • Printing Variables

Printing Variables

July 9, 2016 Leave a Comment Written by Keith Atkinson

If we want to see what happens to the values as a computer runs through the program, we can use a function called print().

print() Function
the print() function writes text to the console area, which is a black box under the text editor (see page 6 for an image). Type the following into the text editor and then click run.

// Declare and initialize the variables 
int firstValue = 5; 
int secondValue = 17; 
int temporary = 0; 
// Print the values in the variables before swapping 
print(“Before swapping\n”); 
print(“firstValue is: ” + firstValue + “ and secondValue 
 is: ” + secondValue + “\n”); 
// Swap the values in firstValue and secondValue 
temporary = firstValue; 
firstValue = secondValue; 
secondValue = temporary; 
//Print the values in the variables after swapping 
print(“After swapping\n”); 
print(“firstValue is: ” + firstValue + “ and secondValue 
 is: ” + secondValue + “\n”); 

The program should print in the console:

Before swapping firstValue is 5 and secondValue is 17 After swapping firstValue is 17 and secondValue is 5

If you looked at the example above, you probably saw a few things that confused you, like quotation marks around certain words, the use of “\n,” and the use of the addition (+) operator.

Strings

In Processing, words are called Strings (with an uppercase “S”). You can tell the difference between a String and a variable name because a String has quotation marks around the word and a variable name has no quotation marks around the word.

New Line

When you are using a word processor, if you want to start a new line of text, you hit the “return” key on the keyboard. To start a new line of text while using the print() function, you need to use the two characters “\n” together with no spaces between the backslash and the letter n. This tells the computer that any text after “\n” needs to be on a new line.

print(“This sentence is on one line\n”); 
print(“This sentence is on the next line”); 
This sentence is on one line This sentence is on the next line

println() Function
The println() function is exactly the same as the print() function except it adds a newline to the end of the output.

println(“This sentence is on the first line”); 
print(“This sentence is on the second line ”); 
println(“ This sentence is not on a new line”); 
print(“This sentence is on the third line”); 
This sentence is on the first line This sentence is on the second line This sentence is not on a new line This sentence is on the third line

Concatenating Text
When you see the plus sign (+) in the print() function, it means something different than what you are used to in math class. The plus sign tells the computer to concatenate, or combine, two strings.

String firstName = “George” 
String lastName = “Washington” 
print(“The president of the US is:” + firstName +
 secondName);
The president of the US is:GeorgeWashington

If you ran the previous example in Processing, you prob- ably noticed that there were no spaces between some of the words. We will have to add our own spaces.

String firstName = “George” 
String lastName = “Washington” 
print(“The president of the US is: ” + firstName +
 “ ” + secondName);
The president of the US is: George Washington
Beginner, Processing
Simple Program
Basic Math

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Comments

    Categories

    • Processing
      • Beginner
    • Scratch
      • Beginner