0

I just don't get it why when I use this,

for(in=1; in<=3;in++) {
    for(out=1; out<=2; out++) {
        console.log('*')
    }
}

it prints 6 stars which seems right to me, And when i use it with if/else like this,

for(in=0; in<=3; in++) {
    for(out=0; out<=2; out++) {
        if(in == 9) {
            console.log('inside');
        }
    }
    console.log('outside');
}

(outside) will be printed 4 times I really don't get it why is it like this ?

3
  • you have in=0; in<=3; in++ which will loop it 4 times... so ouside is printed 4 times.... Commented May 22, 2015 at 1:25
  • It's not in the if statement, so why shouldn't it be printed every time? Commented May 22, 2015 at 1:26
  • Thank you that makes it a little bit clear for me, but what happened now with out=0; out<=2; out++ ? Commented May 22, 2015 at 1:27

1 Answer 1

5

console.log('outside') is inside the first loop which goes from 0 to 3, so it's printed four times. 'inside' is never printed because in never reaches 9

Sign up to request clarification or add additional context in comments.

1 Comment

Your if statement does nothing because in never reaches 9. Since there's nothing else inside the inner (out) loop, the loop is useless as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.