Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 3. For loops
- Going backwards in an array -> More than or equal to zero, decrement the variable, start array at length - 1
- Nested for loops -> Everytime you have 2 for loops, the outer one is run once and the inner is run all the way through
- 4. Loops
- Array indexes account for zero
- When finding out which index is greatest, use a for loop and compare the indexes
- int[] example = {5, 7, 3, 9, 20, 4};
- void setup() {
- int maxIndex = 0;
- for (int i = 0; i < example.length; i++) {
- if (example[i] > example[maxIndex]) {
- maxIndex = i;
- }
- }
- }
- void draw() {
- }
- int[] example = {60, 90, 50, 30, 60, 100};
- int numPass = 0;
- int numFail = 0;
- void setup() {
- int maxIndex = 0;
- for (int i = 0; i < example.length; i++) {
- if (example[i] >= 70) {
- numPass++;
- }
- else {
- numPass--;
- }
- }
- }
- void draw() {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement