Burnie - 5 months ago 31

Java Question

Are these basically the same?

1) While

`int i = 0, j = 0;`

while (i < 10) {

while (j < 10) {

} j++;

} i++;

2) For

`for (int i = 0; i < 10; i++) {`

for (int j = 0; j < 10; j++) {

}

}

Still kind of new to programming and I just wanted to know. Which one would be more efficient if they are the same?

Answer

No, they are not the same. 1 is wrong, and you will stuck in the while loop since the j++ is out of the block. If you want to make 1 doing the same thing as 2, should change to:

```
int i = 0, j = 0;
while (i < 10) {
j = 0;
while (j < 10) {
....
j++;
}
i++;
}
```