How to put while loop inside for loop


#1



Infinite loops.
Hay guys,
Can anybody help with this. I want to put while loop inside for loop. I tried to do it and always had to reload the page. So, if you can explain what's my mistake here - I'll appreciate.

Cheers,
Igor.


for (var a = 0; a <= 5; a++) {
    console.log(a + ". for loop in action!");
    while ( (j = a) && (j === 2) ) {
        console.log("And here is a while loop! Line: " + a);
        };
};


#2

I think I'll solve it.

Currently, coded this stuff:

// Write your code below!
var a = 0

for (a; a <= 10; a++) {
console.log(a + ". for loop in action!");
while (a <= 5) {
console.log(a +"(while loop). And here is a while loop!");
a++;
do {
console.log(a + "(do..while). ongoing");
} while(a === 8);
};
};

The output is:
0. for loop in action!
0(while loop). And here is a while loop!
1(do..while). ongoing
1(while loop). And here is a while loop!
2(do..while). ongoing
2(while loop). And here is a while loop!
3(do..while). ongoing
3(while loop). And here is a while loop!
4(do..while). ongoing
4(while loop). And here is a while loop!
5(do..while). ongoing
5(while loop). And here is a while loop!
6(do..while). ongoing
7. for loop in action!
8. for loop in action!
9. for loop in action!
10. for loop in action!

Still, no expected result, but it'll be.


#3

this:

while ( (j = a) && (j === 2) )

the moment a becomes two, the while loop is infinite. in the while loop, the condition won't change, so j will stay 2 forever.


#4

What result did you expect? Programs only do what you program it to do. Maybe you should get some more practice with loops? And read first before trying


#5

Thanks for showing me my mistake.


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.