+ 1

Can some one explane a for loop to me?

21st Jan 2017, 5:10 PM
kelogames
kelogames - avatar
6 Respostas
+ 4
It's a looping process very effective when you want to manipulate data, usually many. Think firstly of the counter, or starting point variable as this is executed before our loop begins. This is our counter. for (x = 0; The test is assessed where if the condition is true, it will enter the code block or else cease looping. for (x = 0; x < 2; The code will execute twice in this case, but we need a something to make the counter increment. for (x = 0; x < 2; x++) In better terms, WHILE x is less than 2, enter code block and before you leave, add 1 to our counter. If x is greater than 2, DON'T enter. I hope I haven't baffled you, and sorry if I have.
21st Jan 2017, 5:39 PM
Mark Foxx
Mark Foxx - avatar
+ 2
@RANJOY KONSAM That's not JavaScript... for (var i = 1; i <= 5; i++) { document.write(i) } // outputs 12345 document.write(i) // outputs 6, because i gets incremented but cannot proceed with the loop, so the loop stops at i = 5.
21st Jan 2017, 5:26 PM
David Sebastian Keshvi Illiakis
David Sebastian Keshvi Illiakis - avatar
+ 2
sure just look at the syntax of a loop: (example) for(var i = 0; i < 5; i++) {...} There are 3 segments var i = 0; this is the first segment and it defines a variable the loop can count with i < 5; this is the second segment and it's a condition, everytime a loop starts again this condition is checked the loop only continues as long this condition is true i++; This is the third segment and is called at the end of the loop and adds 1 to i. So we defined "i" as 0 then the loop starts and and the condition is checked. in this case it's true because 0 is smaller than 5. Your code would run here after all of your code "i" would be increased by 1 "i" is now 1. the condition is checked again "i" is still smaller than 5 your code runs again "i" is increased by 1 again and the condition is checked again ... ... It works like this until I is bigger than 5 or is 5
21st Jan 2017, 5:27 PM
Daniel S
Daniel S - avatar
+ 1
A loop statement allows us to execute a statement or group of statements multiple times . consider this for loop for(int i =1; i<=5; i++){ printf ("℅d ",i); } here the output will be 1 2 3 4 5 simply it means happening again and again or repeating
21st Jan 2017, 5:25 PM
RanjOyHunTer
RanjOyHunTer - avatar
+ 1
oh didn't consider the tag and simply give an example. my bad
21st Jan 2017, 5:27 PM
RanjOyHunTer
RanjOyHunTer - avatar
0
Thanks:) ;)
21st Jan 2017, 5:33 PM
kelogames
kelogames - avatar