# Can someone help me? Nested for-loop

I want to combine these two lines but I don’t know how to.

for (int x=940; x<1001; x+=30);
for (int y=53; y<234; y+=30)

1 Like

Hello, your mistake might be the ; after the first line

Delete this

2 Likes

Chrisir is right, the semicolon ends the first statement so you have two single loops rather than a double loop. I also suggest that you use { } to delineate the code blocks like this

``````for (int x=940; x<1001; x+=30) {
for (int y=53; y<234; y+=30) {
// code to be executed in double loop
}
}
``````

Although the {} are not required if the code to be executed is a single statement I suggest you use them anyway because they

• make it clear which code is included in the loops
• make it easier to add / remove / change debug code inside the loops
• reduces the risk of logic errors
4 Likes

typical example

``````
void setup() {
size(1200, 900);
}//func

void draw() {
for (int x=940; x<1001; x+=30) {
for (int y=53; y<234; y+=30) {
rect(x, y, 20, 20);
}
}
}//func
//
``````
3 Likes

The problem is the `;` at the end of the 1st `for` loop, which is making it a an already complete statement block, w/o incorporating the 2nd `for` loop:

``````for (int x = 940; x < 1001; x += 30)
for (int y = 53; y < 234; y += 30) {
}
``````

Another way (my fav btW):

``````for (int x = 940; x < 1001; x += 30)  for (int y = 53; y < 234; y += 30) {
}
``````
2 Likes

Hello,

Welcome to the Processing Community

Some resources:
https://processing.org/

https://processing.org/reference/for.html

`:)`

1 Like