UAL CCI: Introduction to Creative Computing Intensive 2022: 🤔 & 🔁: Lecture 4: Deciding and Looping.
Back to slide index.
By the end of this lecture, we'll have learnt about:
- Logic in humans and computers
- Some more about functions, namely recursion (a function that calls itself)
- How to use logic and functions together in p5.js code
- How to use everything we've learnt so far to make real time interaction happen using p5.js
- Iteration - which is the obsfucatory term for repeating something several times
- Loops - another word for iteration and the term you'll hear the most in coding
- The four different kinds of loops in p5.js
- A whole series of examples of looping in p5.js
- The concept of scope in coding, and particularly in p5.js
Computers use something called logic to decide what to do. Coding is all about refining sets of rules and consequences to tell a computer how to decide what to do and what do once the decision has been made.
This example demonstrates the use of an if statement. Let's try it cut and pasted into the p5.js editor. It's important to use { and } to enclose both functions AND if (or switch) statements. Those curly brackets help the computer decide something called
scope, which is all about what values variables have.
- Remember from previous lecture - instead of writing long lists of commands, one after another, functions allow programmers to package up functionality into smaller mini programs, known as functions or subroutines.
- Functions take input(s) and return output(s) - an example might be a function that returns the square of an input number, or as complicated as the answer to everything.
- Recursion is when something is defined in terms of itself. Recursive humour is funny. Recursion is also found in graphic design, art and cooking. In computer science recursion is when a function calls itself repeatedly. The method of working out the Factorial of any number is a classic example.
- A fractal is a good example of something that is visually recursive.
Let's look at a
p5.js numbers example to refresh our memory on the use of if statements and logical comparisons to decide how a program should behave.
To finish up with, let's work through
the p5.js tutorial on interactivity - combining all we've learned about using variables and logic to make experiences that react to users in real time. As I always say, "real time or it didn't happen" - or if that was pre-rendered, it's not interactive, it's just a film!
Iteration or looping or repeating is one of the most powerful tools for getting things done in coding. Loops let you repeat the same recipe or set of instructions or commands or block of code as many times as you like. You can set loops to run a set number of times, or for a certain period of time, or to run for every piece of information in a certain place (like for every entry in an address book) or even to run an interactive number of times (until the user presses a certain key for example).
Let's look at the definition of
Iteration - the
obsfucatory term for repeating something several times.
Loops are just a part of the wider coding world of
Control Flow. We've already encountered some control flow with if/else statements and switch statements. Another way of thinking about loops is doing things many times, but using logic to stop. Boolean logic in our case.
Let's venture into the
p5.js tutorial on program (or control) flow, specifically the section on four different kinds of loops in p5.js. BTW - the structure of for loops is going to take a bit of time to get used to - before you know it you'll be typing them out automatically!