Programming Fundamentals with Processing
© 2012-2018 Mithat Konar.
All rights reserved.
Part One
Part Two
Part Three
- Action
- Interaction
- Bundling Stuff Up II
Misc and cruft
- Needs a place
- strings and string processing (Appendix?)
© 2012-2018 Mithat Konar.
All rights reserved.
This is an old revision of the document!
Operations and expressions
In this chapter, we will take a closer look at what makes up the commands you can use in Processing.
Processing is what computer science people call an imperative language. The essence of an imperative language is that programs written in it are a sequence of commands that the computer performs. Furthermore, those commands may (and typically do) change the state of the program. Here is an example of Processing code that commands three state changes (all on the variable foo
):
int foo = 55; foo = 99; foo = 42;
In Processing (as in many other imperative languages) you can think of commands as consisting of a hierarchy of concepts (from lowest-level to highest level): operations, expressions, and statements.
In a previous chapter, we said that statements are the smallest complete executable element in an imperative language. Statements are often built up from expressions, which are themselves built up from operations. In Processing, a semicolon is used to mark the end of a statement, as in the examples below:
int theAnswer = 42; println(theAnswer);
We will talk about operations first and then discuss expressions.
You are probably familiar with operations in mathematics. Operations are made up of operators and operands. Operators indicate what you want to do and operands are the things you want that operation done to. In the following operation:
$12 + 30$
the operator, represented by the plus symbol, indicates what you want to do with the operands 12
and 30
. The concept of an operation in imperative languages is taken directly from the concept in mathematics—although the notation and details can and do differ.
Processing defines a number of arithmetic operators that work essentially the same as in pure math. These are summarized in the table below.
Operator | Description | Example |
---|---|---|
+ | addition | a + b |
- | subtraction | b - d |
- | negation | -b |
* | multiplication | c * d |
/ | division | d / c |
% | modulus | d % c |
Processing uses infix notation for arithmetic operations, meaning that the operator is placed between the two operands.1)
The +
operator is used for addition. The two operands must be (or evaluate to) numeric values.
foo + 5 foo + bar
TODO
TODO
TODO
TODO
Assignment is an operation on two operands that sets the value of one of the operands (typically a variable) to the value of the other operand. In Processing, the equals symbol is used to indicate assignment. In the example below, the value 8 is assigned to foo
, then twice the value of baz
is assigned to bar
.
foo = 8; bar = 2 * baz;
The assignment operation in Processing looks like the “equals” relationship in math. This is can be a source of confusion to new programmers, but they are not the same thing! “Equals” is a statement of fact: two things are the same. Assignment is an operation: it does something—specifically, it copies the value of whatever is on the right into what is on the left.
Assignment is one way that the state of a program can be changed.
TODO
Operators can be categorized based on the number of operands they require.
Unary operators take one operand. An example of a unary operator is the logical NOT: !foo
.
Binary operators take two operands. The arithmetic operators above are all examples of binary operators as are the logical operators, with the exception of the logical NOT.
Ternary operators take three operands. Processing's conditional operator (not discussed here) is an example of a ternary operator.
Theoretically, it's possible for a language to define an operator with an arbitrary number of operands, but I am not aware of any operators that take more than three operands in Processing.
Expressions are made of up one or more operations and return a value.
TODO
TODO
TODO
(Spaces, tabs, and the invisible characters that mark the end of a line. In short, whitespace in Processing (and Java, C++, and C) is interchangeable. In other words, from Processing’s point of view, a space is the same thing as a tab is the same thing as the end of a line.)