Stopping for(…) loops
for loop is known as the stopping condition. It is used to determine whether to execute the statements in the code block or whether to exit the loop.
If the expression evaluates to
true, the statements in the code block are executed. If the expression evaluates to
false, the stopping condition has been met and the loop is exited.
for loop can iterate “in reverse” by initializing the loop variable to the starting value, testing for when the variable hits the ending value, and decrementing (subtracting from) the loop variable at each iteration.
array.length will return the exact length of the array in question. This is extremely helpful for looping through arrays, as the
.length of the array can be used as the stopping condition in the loop.
for loop runs inside another
The inner loop will run all its iterations for each iteration of the outer loop.
while loop creates a loop that is executed as long as a specified condition evaluates to
true. The loop will continue to run until the condition evaluates to
false. The condition is specified before the loop, and usually some variable is incremented or altered in the
while loop body to determine when the loop should stop.
do...while statement creates a loop that executes a block of code once, checks if a condition is true, and then repeats the loop as long as the condition is true. They are used when you want the code to always execute at least once. The loop ends when the condition evaluates to false.
break keyword may be used to exit the loop immediately, continuing execution after the loop body.
for loop declares looping instructions, with three important pieces of information separated by semicolons