Variables Data Types: Integer, float, boolean, String Example: String name = "Guzy"; int score = 12; float percent = 0.92; println(grade1 + grade2 + grade3 + grade4 / 4) VS println((grade1 + grade2 + grade3 + grade4) / 4) println(name + grade1 + grade2); All will be combined as Strings, the grades won't add like integers VS println(name + (grade1 + grade2)); Use BEDMAS where needed to control how variables are combined ------------------------------------------------ Conditionals if statements if (condition 1) { } else if (condition 2) { } else if (condition lots) { } else { } All conditions are boolean expressions, meaning they evaluate to TRUE or FALSE Example: x > 12 is a boolean expression, x > 12 is a TRUE or FALSE question If Block structure. 1) Starts with: if ( condition ), ex. if(x > 12) 2) Can have as many else ifs as you need. There is no practical limit. 3) You can optionally have 1 single, final, else statement. 4) The first condition to evaluate to TRUE will trigger the block immediately. Once it is triggered, the computer will not even read the remaining code. This means that even if multiple conditions are TRUE, they will NOT be run, only the first one will execute. Example: if (condition 1) { fill(255, 0, 0); rect(0, 0, width, height); } Boolean Math Operators: AND, OR, NOT AND: RULE: if both are TRUE --> return TRUE, otherwise return FALSE --> Symbol is: && Example T && F = F T && T = T F && F = F OR: RULE: If either is TRUE, evaluate TRUE. Symbol: || Example: T || F = T F || F = F T || T = T NOT: RULE: Just flips a TRUE to FALSE, or a FALSE to TRUE Symbol: ! !(F) = T !T = F BEDMAS APPLIES --> Just do it 1 step at a time just like in math class. T && (F || T) = T && ( T ) = T Two exceptions to the "show your steps rule" Anything OR'd with TRUE is TRUE T || (crazy time) = T Anything AND'd with a FALSE is FALSE (more crazy) && (even crazier) && F = F Comparisons > < >= <= == x > 12 etc Remember 5 > 5 is FALSE, 5 >= 5 is TRUE. Just a little thing people get stuck on sometimes. BEWARE, there is a huge difference between = and == = is an assignment statement, it compares nothing! x = 12 makes x a 12, even if x was something else before. == will compare properly, never use single = in an if condition, its a classic mistake. ------------------------------------------------------ Loops This will repeat until the condition is FALSE. Yes, that means it can run forever. Use this loop if it isnt obvious when the loop should shut down, example a password checker while(condition){ } Example: Keep picking numbers UNTIL you get 5 or less while ( x > 5){ x = floor(random(10)); } while (password != "thePassword"){ password = random(); } For Loop: Runs a very specific number of times. for(initial value; condition; step) for(int counter = 0; counter < 4; counter = counter + 1){ } ------------------------------------------------- Arrays Arrays : fixed size int[] scores = new int[20]; scores[0] = 12; scores[1] = 34; ... scores[19] = 45; for(int i=0 ;i < scores.length; i++) println(scores[i]); You cannot change the size of the array, or go beyond its bounds. Remember, for all Array (and ArrayLists) the first index is zero. 0 1 2 3 4 is an array of size 5 ---------------------- ArrayList Super flexible, but a bit slower to operate and the syntax is a bit stranger / harder to work with. ArrayList scoreZ = new ArrayList(); <-- Don't forget the () at the end To add values to the list use: .add(value) scoreZ.add(4); scoreZ.add(7); scoreZ.add(9); To change a value: scoreZ[2] = not possible, thats fixed sized Arrays, we are using ArrayList USE: .set(index, newValue); Example: scoreZ.set(2, 12); To access values: USE: get(index); Example: println(scoreZ.get(3)); Crazy difficult Example: Assume ArrayLists for X, SX already exist and have values and such. X.set(index, newValue) for(int i=0; i < X.size(); i++){ X.set(i, X.get(i) + SX.get(i)); } If it were fixed sized arrays, that line would look like X[i] = X[i] + SX[i] ------------------------------------------------------ Functions Any variables declared INSIDE the function or defined as input arguments only exist for the duration of the function call. Those variable names have NO meaning outside the function. They have what is called LOCAL SCOPE Variable you declare at the top of your program and use everywhere have GLOBAL SCOPE 3 Types of functions possible 1. That needs nothing, provides nothing. It works in silence. void means it return no value. It calcuates nothing at all. It only does a single job. void drawBackground(){ fill(0); rect(0,0,width,height); } Example usage: drawBackGround(); 2. Needs Something, provides nothing. tonee is an argument for the function, ie a bit of information is needs to do its job void drawBetterBack(int tone){ fill(tone); rect(0,0,width,height); } Example Usage: drawBetterBack(180); Anoterh version for RGB colour because someone in class asked the question. Good job someone! void drawBetterBackColour(int red, int green, int blue){ fill(red, green, blue); rect(0,0,width,height); } Example Usage: drawBetterBackColour(20, 80, 190); 3. return values, either with inputs or without This function returns an integer value, so you better calculate one and return it. This function takes in two integers and return the highest value. int getHighest(int a, int b){ int result = a; if(b > a) result = b; return result; } Example Usage: int bestNumber = getHighest(80, 70); Other various examples because people asked. Good job people! int rando(){ return floor(random(100)); } hogwartsCorrect = rando(); Input an array void useArray(int[] numbers){ for(int i=0; i < numbers.length; i++) println(numbers[i]); } Inputs an ArrayList, returns a fixed sized Array int[] makeArray (ArrayList nums){ int [] result = new int[nums.size()]; for(int i=0; i < nums.size(); i++) result[i] = nums.get(i); return result; }