Is a for loop always executed at least once?

According to my teacher, for-loop is always executed at least once, even if the condition is not met.

Example (for example, I know this from C ++):

for (int i=6; i <=5; i++) { //irrelevant for this question } 

According to her, this cycle will be performed at least once, but is it not, or am I missing something? Is there any case, no matter what language it will be run once? To eliminate the idea in advance: yes, it was about cycles, not about making loops.

Edit

Thanks for all these quick answers, I think this case is already closed. Good day / night.

+5
source share
4 answers

You can say that for-loop is always evaluated at least once.

But if the for-loop condition is not met, its block will never be executed.

Since you did not ask about other cycles, I will not address them.

+6
source

A cycle will be executed only when its condition is true. Because the for loop and while loop check the condition before the body is executed, they will never be executed if the condition is false.

The only loop that will be is the while loop. With a do while loop, the condition is not evaluated until the end of the loop. Because of this, the while while loop will execute at least once.

+2
source

A for-loop always ensures that the condition is true before running the program. Whereas the do-loop runs the program at least once, and then checks the condition.

+1
source

A position-driven contour will never be executed if the condition is false , however, exiting the controlled loop will be performed at least once.

0
source

Source: https://habr.com/ru/post/1269531/


All Articles