Could someone help me with for loops | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Could someone help me with for loops

*PLEASE EXCUSE THE CAPS LOCK iT WOULDNT GO AWAY AND CODE GRAMMER AS IM ON AN IPAD* IM NEW TO JAVASCRIPT AND IM FINDING it HARD TO UNDERSTAND FOR LOOPS I UNDERSTAND THE FOLLOWING LOOP SORT OF VAR I = 1 FOR (I=1; 1<=5; I++) { DOCUMENT.WRITE(I + ‘<BR/>’); } THIS WILL PRINT 1-5 WITH BREAKS BUT WHERE IM TRULY LOST IS IN THIS CODE BLOCK VAR X = 0; FOR (; X<= 20; X += 2){ DOCUMENT.WRITE (X); } WHY IS THERE AN ; BEFORE THE X IN THE FOR STATMENT ALSO WILL THIS PRINT AS ONE LINE WITH NO BREAKS AS I DIDNT ADD THE <BR/> AT THE END. ANY HELP WITH BE AMAZING THANKS

23rd Jan 2020, 1:52 PM
Lewis Casey
Lewis Casey - avatar
4 Answers
+ 2
within the for loop there are three conditions needed. so your for loop will be like so: for(1st; 2nd; 3rd){ do something each iteration } 1st condition: set a variable name and choose whether it has a value. usually this is “i” and it is normally set to 0 but it isnt a law. just normal to see it this way. 2nd condition: set a rule about the variable in the first condition. if you set the variable to i in first condition, and if you want to loop 10 times, then second condition will be like “i < 11” or “i <=10” (there can be many ways to say the same thing with code). 3rd condition: decide what to do with “i” (your variable) after one iteration is finished before it starts the next iteration. For example, if we want to loop to 10, we should make i go up one number every time until it is 10 or higher. if we dont say this, the loop will go forever because i doesnt change. we must tell the loop to increase i. so third condition is like “i++” so it will go up by one number every time. I hope this can help you.
23rd Jan 2020, 2:16 PM
bright_byte
bright_byte - avatar
+ 2
your code block you posted, for example, the first “;” is omitted because the variable is named before the for loop. if it isnt named, it will break the for loop. I did mot test this, but if you remove the line “var x = 0;” then try to run, this for loop will fail. If it fails, you can put the same “var x = 0” inside the for loop. so it will say, “for(var x =0; x <=20; x += 2){ document.write(x); }” and then it will work again.
23rd Jan 2020, 2:21 PM
bright_byte
bright_byte - avatar
+ 1
for loops have 3 places for statements between parentheses ( ), they have all their own meanings. First will be ran once in the start of the loop. Second is a condition which determines when to break the iteration. Last is a statement that will be ran after each iteration. It makes very it easy to make a counter to count from x to y with a step of z. But they can also be omitted, it is not necessary to have any of these, but it must be marked with ; . In: for (; x <= 20; x += 2) There is no statements in the setup part for the for loop.
23rd Jan 2020, 2:06 PM
Seb TheS
Seb TheS - avatar
0
Because it's the syntax of it. for(initialization;condition;increment){} It doesn't matter if you initialize or add condition or increment separately, the syntax stays the same.
23rd Jan 2020, 2:26 PM
Srishti Chhabra
Srishti Chhabra - avatar