简体   繁体   中英

Nested for loops with if/else in Javascript

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM