We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › WATS "two nested for loops" mean
Page Index Toggle Pages: 1
WATS "two nested for loops" mean? (Read 652 times)
WATS "two nested for loops" mean?
Oct 19th, 2009, 2:20pm
 
IM LOOKING FOR HELP UNDERSTANDING "two nested for loops"

DOES IT MEAN one loop is included inside the other one?

AND IF SO IS MY CURRENT *****BASIC****** CODE RIGHT?

ANY HELP MUCH APPRECIATED.

CODE:

size(500,500);
noFill();
smooth();
background(0);

for(int x=0; x<500; x=x+60) {       //DOTS
 for(int y=0; y<500; y=y+8) {
   stroke(300*x,300*y/width,128);
   point(x, y);
 }
}

noFill();
for ( int x=8; x< width; x+=10) {    //Outer Spheres L-R
float n= norm( x, 0 ,width ) ;
  float y= radians (n);
  strokeWeight (n*4);
  stroke(355*n,55*y/width,128);
  ellipse ( x,y,-0.3*width,1.6*width);
}

for ( int x=8; x< width; x+=76){   //Inner Rects L-R
  float n= norm( x, 0 ,width ) ;
  float y= radians (n);
  stroke(200*n,6000*y/width,1000);
  y *= width;
  strokeWeight (n*10);
  rect( x,y,-0.1*width,0.75*width);
}





Re: WATS "two nested for loops" mean?
Reply #1 - Oct 19th, 2009, 3:34pm
 
DOES IT MEAN one loop is included inside the other one?

Yes

AND IF SO IS MY CURRENT *****BASIC****** CODE RIGHT?

If you mean:

Code:
for(int x=0; x<500; x=x+60) {       //DOTS
for(int y=0; y<500; y=y+8) {
  stroke(300*x,300*y/width,128);
  point(x, y);
}
}


then yes.  This may help to clarify:

Code:
int limit1 = 3;
int limit2 = 4;
PFont font;

void setup() {
 size(300,400);
 
 font = createFont("Arial", 16);
 
 // Creates 3 columns with 4 rows
 // outer for loop
 for(int i=0; i<limit1; i++) {
   // inner 'nested' for loop
   for(int j=0; j<limit2; j++) {

     fill(255);
     rect(i*100,j*100,100,100);
     textFont(font, 16);
     fill(0);
     text(i*limit2+j, i*100+44, j*100+50 );
     
   }
 }
}


Page Index Toggle Pages: 1