====== 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. 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. ===== 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%%''| |''%%-%%'' |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.((Alternatives to //infix// used by some languages are //postfix// (operator after operands) or //prefix// (operator before operands) notation. )) === Addition === The ''%%+%%'' operator is used for addition. The two operands must be (or evaluate to) numeric values. foo + 5 foo + bar === Subtraction === The ''%%-%%'' operator is used for subtraction. The two operands must be (or evaluate to) numeric values. foo - 5 foo - bar === Negation === The ''%%-%%'' operator is also used for numeric negation. It operates on one operand that must be (or evaluate to) a number. It evaluates to the numeric inverse of the operand. -foo -(2 * foo) === Multiplication === The ''%%*%%'' operator is used for multiplication. The two operands must be (or evaluate to) numeric values. foo * 5 foo * bar === Division === The ''%%/%%'' operator is used for division. The two operands must be (or evaluate to) numeric values. The behavior of the ''%%/%%'' operator varies depending on whether the operands are integer or floating-point types. == Floating-point division == When at least one of the operands in a ''%%/%%'' operation is a floating-point type, the result will also be a floating-point type and have the expected value to the extent possible by the limited precision of floating-point math. (Computers do not have infinite precision, so in general small errors will occur.) float x = 2.2; int y = 2; double z; z = x / y; The result of the above is that ''%%z%%'' holds the value 1.1 (or a value very close to that). == Integer division == When both operands in a ''%%/%%'' operation are integer types, the result will be an integer type and truncating division will take place. In truncating division, the fractional part of the result (i.e., the stuff after the decimal point) will be thrown away. The result will not be rounded to the nearest integer value; whatever the fractional part happens to be will simply be eliminated. int x = 5; int y = 3; int z; z = x / y; The result of the above is that ''%%z%%'' holds the value 1. === Geekbreak: WTF division? === Who on earth thought that truncating division would ever be a good idea? In the early days of computing, floating-point arithmetic was a very costly proposition. You either needed to tie up the central processing unit with complicated algorithms to churn out floating-point results or you needed to install dedicated floating-point math hardware (typically referred to as floating-point units). These were often expensive and power hungry beasts. So, it was natural in programming languages to specify two ways of doing the most costly of the floating-point operations: division. Today, all but the most basic processors have built-in floating-point units, so floating-point math isn't nearly as big an issue as it used to be. But it still takes many processors less time to perform integer math operations than it does floating-point operations. Some modern languages, like Python 3, define //all// division as floating-point division by default. If you want to perform truncating division, you have to explicitly ask for it. JavaScript takes this one step further: it doesn't differentiate between floating-point and integer types at all; that is, in JavaScript there is only one numeric type. The potential performance advantage of integer math is particularly important in real-time processes---including animations. Therefore, it's a good idea in Processing to be in the habit of not using floating-point variables and operations unless you have a good reason to do so. === Modulus === The ''%%%%%'' operator is used for the modulus operator. The modulus operator operates on two operands that must be (or evaluate to) integer (not just numeric) values. Despite appearances, this operation has nothing to do with percentages. The operation produces the //remainder// that results from the division of the two operands. int x = 5; int y = 3; int z; z = x % y; The result of the above is that ''%%z%%'' holds the value 2. ((To be pedantic, there is a difference between modulus and remainder, but it only applies to negative numbers. FIXME)) === 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. 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. ------------------------------------- ==== Mixing numeric types ==== FIXME TODO ==== Logical operators ==== FIXME 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. FIXME TODO ==== Order of precedence ==== FIXME TODO ==== Whitespace ==== FIXME 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.)