For loops, can I invoke them too?

a question came up, can I choose to call for loops when I want to, as I do with functions? or do they get executed only when the file is run in the terminal?

loops are executed when they are encountered in the execution of the file.

If you want to write a loop before the point of the code in which it is needed, you can put it in a function

1 Like

They’re statements, not expressions, so no, you’ll get a syntax error if you try.

Edit: I’m assuming you mean can you do something like

const do = for (let i = 0; i < 5; i++) {

Then use do somewhere else in your program?

this is new to me, i will try it.

No, it won’t work, that’s what I’m saying, for loops (or while loops or do…while loops or if statements) are statements, they do something to your code when that part of the code is reached

1 Like

i found out later that i can put it in a function and call the function when i want.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.