Is a For Loop always executed at least once?
You could say a for-loop is always evaluated at least once.
But if a for-loop's condition is not met, its block will never execute.
Because you didn't ask about other loops, I won't address those.
A loop will only execute while its condition is true. Since a for
loop and a while
loop both check the condition before the body is executed they will never execute if the condition is false.
The only loop that will is a do while
loop. With a do while
loop the condition is not evaluated until the end of the loop. Because of that a do while
loop will always execute at least once.
A for-loop always makes sure the condition is true before running the program. Whereas, a do-loop runs the program at least once and then checks the condition.