Atlassian uses cookies to improve your browsing experience, perform analytics and research, and conduct advertising. Accept all cookies to indicate that you agree to our use of cookies on your device. Atlassian cookies and tracking notice, (opens new window)
Confluence
For you

Programming
Results will update as you type.
  • Tiling and Repetition
  • Reactors
  • Programming Basics: Parametric and Generative Graphic Design 2016
  • Archive
  • High Scores
  • Artificial Neural Network
  • Alternatives to the Processing IDE
  • p5.js Programming
    • p5.js Introduction
    • p5.js Variables
    • p5.js Random Numbers
    • p5.js SVG + Images
    • p5.js WebGL
    • p5.js Classes and Objects
    • p5.js Events and Functions
    • p5.js Loops
      • P5js Schleife (de)
    • p5.js Coordinates
    • P5js Nested Loops
    • p5.js Animation Exercise 1
    • p5.js Animation Exercise 2
    • p5.js Conditionals
    • p5.js Arrays and Lists
    • p5.js Simple Collision Detection
    • p5.js Reactors
    • p5.js Tiling and Repetition
    • p5.js Vectors
    • p5.js Animation Solution with Objects
    • p5.js Easing
    • p5.js Perlin Noise
    • p5.js Particle System
    • p5.js Sound
    • p5j.s Typography
    • P5js Archive
  • Programming in Processing (java)

/
p5.js Loops
Updated Sep 23, 2021

p5.js Loops

Analytics

Loading data...

A loop is a control statement that allows us to repeat a code block. Loops contain a conditional which determines how many times the code block is repeated.

There are few different loop types:


For-loop

The for loop loops through a block of code a specified number of times.

 

for([variable declaration], [condition], [increment step]) {

[code block] 

}

for(let i=0; i <= 100; i++) { print(i); }

 

For-In-loop

The for in loop loops through the properties of an object.

 

for([variable declaration], [condition], [increment step]) {

[code block] 

}

let person = {name:"Maik", surname:"Muster"}; let x; for (x in person) { print(x) }

 

While-loop

The while loop cycles through a block of code as long as its specified condition is true.

 

while ([condition]) {

 [code block]
}

let x=0; while(x <= 100) { print(x); x++; }

 

Do-While-loop

The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, it will then repeat the loop as long as the condition is true.

 

do{
} while([condition]);

let x=0; do { print(x); x++; } while(x <= 100);

Exercise

Write a program that creates a line of animated icons on the screen using a loop. Make use of the sin() function to create a simple oscillation.    

Related content
p5.js Programming
p5.js Programming
Luke Franzke
p5.js Variables
p5.js Variables
Luke Franzke
p5.js Introduction
p5.js Introduction
Luke Franzke
p5.js Random Numbers
p5.js Random Numbers
Luke Franzke
p5.js Particle System
p5.js Particle System
paulina.zybinska
p5.js WebGL
p5.js WebGL
paulina.zybinska
{"serverDuration": 14, "requestCorrelationId": "2d0ac78660bd4bfd9181e171a9e5e354"}