Is main(); function included as loop statement? for example i have a program with subprograms then if I finished a subprogram i will use main(); function to go back from the main program. is that considered as loop?
thanks!!

I am not sure that I understand your question completely.

A loop is a programming construct that repeatedly executes some code while a certain condition is true. Examples are the "for" and "while" constructs.

If you are asking whether something like this is possible:

for(;;) {
    main();
    program();
}

then I would have to say "No". The main() function is the entry point for an application; each program would have its own main() function. On the other hand, something like this should be possible:

#include <stdlib.h>

int main(void)
{
    for(;;) {
        system("program1");
        system("program2");
    }
}

where "program1" and "program2" are different programs.

I think I understand your question a bit better, now that I have re-read it.

Calling main() from within main() would be an example of recursion, and not looping. Even so, I don't think recursion on main() is a very good idea. From what I know, it is outright illegal in C++.

function to go back from the main program. is that considered as loop?

its not loop. Its just "calling the main function" we don't consider it a loop.

main() is the function from where compilation begins.
So if you call main() within main(), then it will go infinitely recursing, and is forbidden in C++.

yes, completely agree with np complete
calling main() in main() causes recursion and which never terminates the program.
some compilers don't even allow this.
If you try this out in Borland C you'll get an error saying

cannot call "main" from within the program in function main().

Calling main() from within main() would be an example of recursion, and not looping.

its not loop. Its just "calling the main function" we don't consider it a loop.

Actually, it's called a recursive loop, so yes, recursion is a loop.

A loop is a programming construct that repeatedly executes some code while a certain condition is true.

Which is exactly what recursion does.

But the rest of the information here (for the most part) is correct enough. You should never call main() as a function.

Edited 4 Years Ago by WaltP

Actually, it's called a recursive loop, so yes, recursion is a loop.

yeah! you are right. It forms a recursive loop.

so yes, recursion is a loop

Correct me if I'm wrong, but my understanding was that a new stack frame would be pushed on to the call stack for each recursive call to a function; this does not happen with a loop construct.

You are correct.

That still doesn't change the fact that recursion is a loop. The term loop depends on code executed, not on data addresses used

This article has been dead for over six months. Start a new discussion instead.