• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

BMS Processing Class

A Course23.com Production

"Stop, Pause, and Think..." -- Ms. Elwell
"Take your time, it isn't a race" -- Mr. Helmke

  • Home
  • Conditionals and Logic
    • Conditionals 1: Boolean Expressions and the If Statement
      • Activity 16 (IF1)
    • Conditionals 2: If/Else Statements
      • Activity 17 (IFELSE1)
    • Conditionals 3: Else If Statements
      • Activity 18 (elseif1)
      • Activity 19
      • Activity 20 (elseif3)
      • Activity 21
    • Logical Operators
      • Activity 22 (And1)
      • Activity 23
      • Activity 24 (SingleRollover)
      • Activity 25 (FourSquareRollover)
      • Activity 26 (MoveWhenPressed)
      • Activity 27 (bouncy ball 1)
      • Activity 28 (bouncy ball 2)
  • Functions
    • Moving Shapes 1
      • Activity 29 (StarBright)
      • Activity 30 (CreatureAtOrigin)
    • Moving Shapes 2
      • Activity 31 (drawCreatureFunction)
      • Activity 32 (BouncingCreatures)
    • Rotating Shapes
      • Activity 33 (RotatingCreatures)
      • Activity 34 (RotatingShapes)
      • Activity 35 (RotatingShapes2)
  • Looping and Arrays
    • Loops 1
      • Activity 36 (ShapeLoop)
      • Activity 37 (RandomLoop1)
    • Loops 2
      • Activity 38 (Deg2Rad)
      • Activity 39 (NumberLoop1)
      • Activity 40 (NumberLoop2)
    • Arrays 1
      • Activity 41
      • Activity 42
      • Activity 43
    • Arrays 2
      • Activity 44
      • Activity 45
      • Activity 46
  • Final Topics
    • Introduction
    • Particle Systems
    • Maze Game
  • Resources

Activity 39

Write a sketch to display numbers from 0 to 20 on the screen. The output should be produced using the text() function. Use the previously provided textHeight() function to place the text on different lines.

Each line should be like:

Number 1
Number 2
Number 3

Use a for loop to display the 20 numbers on 20 separate lines.

Now add another loop to your code that displays the numbers in reverse order from 20 down to 0. (So now there are two lists of numbers on your screen. Same output requirement for this as for the first loop).

Now add another loop to your code that displays only the even numbers from 0 to 20. (Now there are three lists of numbers on your screen. Same output requirements for above). Hint: Remember that each of the 3 statements in the for loop parenthesis section are full statements. They can be more complex than we have shown so far.

In previous assignments, you have probably been using the loop counter to keep track of which line you are on for display purposes. If you have not or don’t understand, PLEASE ask Ms Elwell or Mr Helmke. That won’t work the same way for the reverse number count or the even numbers because your line count won’t go down the screen. Try it and see?

An alternate way you can do it is like this:

1
2
3
4
5
var line = 0;
for(var i=0; i < 5; i++) {
  text("num " + i, 200, 200 + line*textHeight());
  line = line + 1;
}

Note this is an example and not quite how you are supposed to do this assignment…

Name your sketch NumberLoop1

Go back to Loops 2

Primary Sidebar

Required Reading:

Course Coding Standards

Copyright © 2016–2021 Chris Elwell/Michael Helmke · This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License · Log in