User Tools

Site Tools


No renderer 'pdf' found for mode 'pdf'
programming_fundamentals_with_processing:ch04-getting-stuff-done-i

Getting Stuff Done I

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.

Statements

In a previous chapter, we said that statements are the smallest complete executable element in an imperative language. In Processing, a semicolon is used to mark the end of a statement, as in the examples below:

int theAnswer = 42;
println(theAnswer);

Statements are often built up from expressions, which are themselves built up from operations. We will talk about operations first and then discuss expressions.

Operations

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.

Arithmetic operators

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
* multiplicationc * 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)

Addition

The + operator is used for addition. The two operands must be (or evaluate to) numeric values.

foo + 5
foo + bar

Subtraction

TODO

Multiplication

TODO

Integer versus floating-point division.

TODO

modulo

TODO

Assignment

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, and this is can be a source of confusion to new programmers. 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.

Mixing numeric types

Logical operators

TODO

The number of operands

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

Expressions are made of up one or more operations and return a value.

TODO

Order of precedence

TODO

Whitespace

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.)

1)
Alternatives to infix used by some languages are postfix (operator after operands) or prefix (operator before operands) notation.
programming_fundamentals_with_processing/ch04-getting-stuff-done-i.txt · Last modified: 2017/07/26 18:06 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki