# I am having trouble converting nested for loops into equivalent while loops anyone know how to do that?

Can you give an example of what you are trying to do so we can give a more specific answer?
answered Oct 9 by (2,327 points)

A for loop uses the same type of logic as a while loop, but it also (usually) includes a counter of some sort. A normal for loop usually looks something like

```for (int i = 0; i < n; i++) {
// do stuff
}```

(where n is a previously defined number, a list length, etc.) Forming an equivalent while loop usually involves taking the first part of the for loop (the initialization) and putting it outside (before) the loop, keeping the second part (the continuation test) as the condition for the while loop, and putting the third part (the update) at the end of the while loop. In other words,

```int i = 0;
while (i < n) {
// do stuff
i++;
}```

When nesting these, you just move one inside the other. In other words, you might convert

```for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
// do stuff
}
}```

to

```int i = 0;
while (i < m) {
int j = 0;
while (j < n) {
// do stuff
j++;
}
i++;
}```

Hope that helps!

answered Oct 9 by (3,831 points)
Thanks! It did