Processing cheat sheet #2 Syntax -- VARIABLES AND EXPRESSIONS -- // Variables must be declared along with their type (int, float, boolean) int n = 5; // creates an integer variable named "n" with initial value 5 float f = 5.0; // creates a decimal variable named "f" with initial value 5.0 -> There are several predefined variables: width and height (of the window), PI, etc. -> Expressions use numbers, variables, +, -, * (multiply), / (divide), and parentheses -> Expressions can be used as arguments to functions -> You can use an int where a float is expected, but not the reverse int[] a = new int[10]; // creates an array of ten integers String s = "Hello, world!"; // double quote only, not single quotes v = e; // sets the variable v to the value of the expression e a[0] = a[9] // copies the last value in array a into the first location boolean a = true, b = false; // declaring a and b; notice case boolean c = (a || b) && !(a && b); // && is 'and', || is 'or', ! is 'not' -- STATEMENTS -- n = 1; // simple assignment; n must have been previously declared boolean logicWorks; // declaration; camelCase used instead of underscore_between_words if (2 + 2 == 4) { // if statement logicWorks = true; } else { // else part is optional logicWorks = false; } while (n < 1000) { // while loop; parentheses, braces, and semicolons are required n = 2 * n; } int sum = 0; for (int element : a) { // for loop using values in an array sum = sum + a; } for (int i = 0; i < a.length; i = i + 1) { /* initialize i to 0; check if i passes test, and if so, do body; then add 1 to i and return to check the test again. Exit when test fails. */ sum = sum + a[i]; } -- METHODS -- int average(int a, int b) { // must declare types of parameters and type returned float two = 2.0; // you can declare local variables return (a + b) / two; // mixing int and float gives a float }