Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Using for loops

From: Interactive Data Visualization with Processing

Video: Using for loops

One of the most common ways of repeating code within Processing and then almost every other computer language is with something called a for loop, and this is a structure that allows you to specify how often the particular code is to be repeated. I'm going to show you a quick example of how this works in creating a drawing in Processing. I'll begin with comment there. I'm going to import a palette from the palette.txt file that's in the Exercise File. This one, I'll be using this palette right here.

Using for loops

One of the most common ways of repeating code within Processing and then almost every other computer language is with something called a for loop, and this is a structure that allows you to specify how often the particular code is to be repeated. I'm going to show you a quick example of how this works in creating a drawing in Processing. I'll begin with comment there. I'm going to import a palette from the palette.txt file that's in the Exercise File. This one, I'll be using this palette right here.

I'm going to copy it, paste it in. I'm also going to create a variable called s for the size of the squares I'm going to be drawing. I'm going to make them 50 pixels on the side. Then I'll do the setup. In the setup I'm going to put the size, 600x200, the background, I'll be putting in, turn on the anti-aliasing and turn off the borders by using no stroke.

So there's the setup. Now normally at this point I would finish the setup and go onto draw, but for this particular one I want to actually go through a couple of loops to fill in the background with some squares. In fact I'm going to be using two loops. Now the way a loop works is you write the word For and then in parentheses, you put a number of pieces of information. The first one is you initialize a variable usually as i, but in this case I'll be using x, because I'm actually going to be incrementing the x variable on this one and I start by saying the initial value of x is 0.

Then you give a test limit and only run through the loop as long as x is less than the width of the window. Every time you run through the loop, take x and add on to it the value of s, which is in this case is 50. So that is the for loop. Now what I'm going to do here is I'm going to come down and create the curly brackets to that block, but I'm actually going to nest another for loop inside this one, because I want to do a grid that goes across and also goes down the window.

So that will be for, but this time y because this is going to be going down. Y needs to be less than the height and Y gets incremented by a similar amount. So what this is going to do is it's going to take the line of x and then go down each step in y. Then it'll go to the line in x and go down each step in y and so forth until it's filled up the entire grid. What it's going to fill it up with is squares of randomly chosen colors.

We're going to use the random palette selection. We've done this a few times already where I specify a fill and then I want to go back to the palette array. Then I need an index number from the array, but I want to use a random number. I have to cast it as an integer variable. That gets a little complicated, and right now I'm just adding the closing parentheses and brackets to finish that off. Then I specify a rectangle. Now this is the easy part. You see that this one, because of the loops I just say drive it x, y and make it s wide and s tall.

And it's going to start with an x and y of 0 and then cycles through 50 pixels at a time, first across the width and then the height. And when I press here's what I get. I get a grid of squares of different shades of blue. If I were to click Play again, it would be a different grid. You see how it's changing each time. But a really cool thing that we can do is once it's filled in is we can go to the draw block, down below, and I can just add this code here.

Now the emphasis in this particular movie is on the for loops and you see that we these nested for loops and how they fill in the grid, and it's a really nice thing to do. This one, I'm also going to get a random fill so I'm going to copy it from right here and then what I'm going to do is I'm going to randomly select values of x. So you see actually I have to declare these again, because the Xs in the for loops were local variables and I made a different block of code.

What I'm doing now is I'm setting up a slightly complicated calculation that will randomly select s, but make it at the beginning of one of the squares in the drawing. I get it by finding how many squares fit in the drawing. That's why I do the width divided by 50 and that's actually going to be 12 and then I get a random number from 0 to 12 and then I turn it into an integer and then multiply that times s to get back up to the original s. It's a little complicated, but it works for what we're doing.

I'm going to do a similar calculation for y where I just change it to y and I change the word width to height, and then finally I'm going bring this code for drawing a rectangle again. And you'll see what this one does is it randomly updates the color of one of the squares in the grid every time. So I'm going to just save this and run it, and it filled in the first time and now it's randomly updating continuously as it goes through. But it was their for loops, the two nested for loops, one for X and one for Y that made it possible to draw the squares at regular intervals in the grid in the very first place and that's one of the great timesavers when you're learning to code.

Show transcript

This video is part of

Image for Interactive Data Visualization with Processing
Interactive Data Visualization with Processing

72 video lessons · 13035 viewers

Barton Poulson
Author

 
Expand all | Collapse all
  1. 3m 16s
    1. Welcome
      58s
    2. What you should know
      1m 22s
    3. Using the exercise files
      56s
  2. 11m 51s
    1. Overview of data visualization
      11m 51s
  3. 11m 53s
    1. Installing Processing
      3m 38s
    2. Overview of Processing
      4m 5s
    3. Exploring libraries
      4m 10s
  4. 1h 1m
    1. Basic setup
      7m 31s
    2. Drawing points
      4m 37s
    3. Drawing lines
      5m 6s
    4. Drawing ellipses and circles
      5m 24s
    5. Drawing arcs
      6m 54s
    6. Drawing rectangles and squares
      4m 58s
    7. Drawing quadrangles
      3m 25s
    8. Drawing triangles
      2m 55s
    9. Drawing polygons
      3m 37s
    10. Drawing simple curves
      4m 54s
    11. Drawing complex curves
      6m 46s
    12. Drawing Bézier curves
      5m 38s
  5. 54m 3s
    1. Introduction to variables
      10m 44s
    2. Understanding variable scope
      6m 53s
    3. Modifying variables
      9m 8s
    4. Creating arrays
      9m 53s
    5. Modifying arrays
      6m 37s
    6. Creating strings
      7m 3s
    7. Modifying strings
      3m 45s
  6. 1h 2m
    1. Incorporating randomness
      7m 59s
    2. Using Perlin noise
      4m 24s
    3. Shuffling with Java
      3m 30s
    4. Specifying line attributes
      8m 2s
    5. Changing placement modes
      5m 45s
    6. Understanding color attributes and functions
      4m 16s
    7. Exploring color spaces
      7m 44s
    8. Using color palettes
      7m 5s
    9. Transforming the grid
      8m 38s
    10. Exploring the attribute matrix
      5m 33s
  7. 52m 7s
    1. Building code blocks
      5m 57s
    2. Writing a while loop
      3m 52s
    3. Using for loops
      5m 35s
    4. Creating conditionals
      14m 50s
    5. Working with easing
      10m 51s
    6. Creating spirals
      11m 2s
  8. 18m 55s
    1. Mouse tracking
      3m 54s
    2. Hovering and clicking
      11m 16s
    3. Understanding keyboard interaction
      3m 45s
  9. 27m 32s
    1. Specifying fonts
      6m 43s
    2. Using images
      5m 51s
    3. Playing a video loop
      6m 20s
    4. Exporting video
      3m 47s
    5. Adding sound
      4m 51s
  10. 20m 49s
    1. Creating functions
      11m 48s
    2. Creating classes and objects
      9m 1s
  11. 31m 10s
    1. Using embedded data
      5m 26s
    2. Working with appended text data
      6m 4s
    3. Working with appended tabular data
      10m 26s
    4. Reading XML data
      9m 14s
  12. 48m 17s
    1. Generating dot plots
      11m 11s
    2. Building scatter plots
      10m 0s
    3. Making line plots
      9m 55s
    4. Creating bar charts
      9m 12s
    5. Checking out examples of maps, hierarchies, and networks
      7m 59s
  13. 20m 57s
    1. Introducing some principles of 2D design
      13m 44s
    2. Understanding color theory
      7m 13s
  14. 24m 46s
    1. Interacting with zooming, rotating, and sliding
      6m 26s
    2. Implementing slicing
      6m 47s
    3. Using rollovers
      5m 58s
    4. Introducing the GUI libraries
      5m 35s
  15. 10m 35s
    1. Sharing via OpenProcessing and other sites
      3m 19s
    2. Saving as a desktop application
      2m 42s
    3. Saving as JavaScript
      1m 47s
    4. Saving as an Android application
      2m 47s
  16. 2m 38s
    1. Where to go from here
      2m 38s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Interactive Data Visualization with Processing.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.