I know a for loops repeat an action some number of times. I read the mdm article on it.
Can someone explain like Im 5?
but I dont understand the syntax, especially the step++ part shown here
for (let step = 0; step < 5; step++) {
// Runs 5 times, with values of step 0 through 4.
console.log("Walking east one step");
}
for loop syntax - C++ Forum
Syntactic sugar in C - (ab)using "for" loops
There is a caveat though. As the author said in the summary section, while the prologue/epilogue example should work in normal conditions, it would break (pun not intended) when you break or goto inside the loop. And break inside the loop happens a lot. Be careful when using this to manage resources. In C++, using a stack allocated object with a destructor in the initialization part can handle exceptional control flows.
What makes for-loops difficult for beginners?
While loop vs. For loops
http://stackoverflow.com/questions/3875114/why-use-a-for-loop-instead-of-a-while-loop
Notably:
More on reddit.comThere is a semantic difference between the two. While loops, in general, are meant to have an indefinite number of iterations. (ie. until the file has been read..no matter how many lines are in it), and for loops should have a more definite number of iterations. (loop through all of the elements in a collection, which we can count based on the size of the collection.)