for (var x=0;x==x;x++) { if (x>5&&x<8) { document.write(x); } } | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
0

for (var x=0;x==x;x++) { if (x>5&&x<8) { document.write(x); } }

what is the output ? how such loops execute? plz explain me..I am confused with middle condition in d above example

1st Dec 2016, 5:24 AM
Tanaji Kolekar
Tanaji Kolekar - avatar
14 Respuestas
+ 8
no problem! Some frustrated guy is down voting all my comments/codes with 5 different accounts lol
1st Dec 2016, 6:36 AM
R2-D2
R2-D2 - avatar
+ 7
What are you trying to do exactly ? for (var x=0;x==x;x++) this loop is wrong. x == x is always true and the loop will never end. So the output will be 6 7 6 7 6 7.... 6 7 forever.
1st Dec 2016, 5:39 AM
R2-D2
R2-D2 - avatar
+ 7
What you are writing is like writing this : int x = 0; while (true){ x++; if ( x > 5 && x < 8) { System.out.println(x);} } Because the condition is always true the compiler will implement x and jump to 5<x<8 and print it. Then again it will jump there and print it and then again and again. If you remove the if statement you will print all integer values and it will not jump to any value. If you change 5 and 8 with some bigger numbers you will notice the jump more.
1st Dec 2016, 6:25 AM
R2-D2
R2-D2 - avatar
+ 5
Yes it will not print anything and it's infinite loop.... if I am correct, you're trying to check the answer for 1 of the question in JS challenge right? for (var x=0;x==x;x++) { if (x >5 || x <8) { document.write (x); break; } } In this case, in the seventh iteration I.e x=6, if condition is true. so it will print 6 and breaks the loop. This is what you're looking for? Otherwise as I said, your code will cause infinite loop and it won't print anything.
1st Dec 2016, 6:14 AM
Venkatesh(Venki)
Venkatesh(Venki) - avatar
+ 1
but if condition inside it is not becoming true then how it will print output.. I'm confused.. explain more
1st Dec 2016, 5:44 AM
Tanaji Kolekar
Tanaji Kolekar - avatar
+ 1
thnx venki
1st Dec 2016, 6:33 AM
Tanaji Kolekar
Tanaji Kolekar - avatar
+ 1
thnx r2d2
1st Dec 2016, 6:33 AM
Tanaji Kolekar
Tanaji Kolekar - avatar
+ 1
i'm lost
30th Dec 2016, 1:14 PM
bruno
bruno - avatar
0
what is this
1st Dec 2016, 6:52 AM
Kevin
Kevin - avatar
0
wow am a beginner in java i will like to learn from you
1st Dec 2016, 6:56 AM
Carlos Dice
Carlos Dice - avatar
0
oh excuse me, I did not know I was in html
1st Dec 2016, 6:59 AM
Kevin
Kevin - avatar
0
The loop is infinite because the condition is always true. Try to put x<9 in the condition, then your code will print your message 2 times.
1st Dec 2016, 11:40 PM
Aly Lema
0
parrotsegurity
4th Dec 2016, 6:13 AM
Mi Camino Es Boxeador Profesional = Jhon / Tux
Mi Camino Es Boxeador Profesional = Jhon / Tux - avatar
0
hacker
4th Dec 2016, 6:13 AM
Mi Camino Es Boxeador Profesional = Jhon / Tux
Mi Camino Es Boxeador Profesional = Jhon / Tux - avatar