It’s almost midnight and I just got a question in my head is “for loop” a statement or a function.
I always thought it is a statement, but I did a google search on it being a function and there are indeed results for that. So what is it? And in that case what is the difference between function and statement?
A for loop is a not usually a function, it is a special kind of statement called a flow control structure.
A statement is a command. It does something. In most languages, statements do not return values. Example:
A function is a subroutine that can be called elsewhere in the program. Functions often (but not necessarily) return values. Example:
A control structure, also known as a compound statement, is a statement that is used to direct the flow of execution. Examples:
Also worth noting is that an expression is a piece of code that evaluates to a value. Example:
All examples are in pseudocode, not tied to any particular language. Also note that these are not exclusive categories, they can overlap.