Flow of execution of programme

Just a simple question...

code:
void example()
{
for(int i=0; i<10; i++)
{
if(condition 1)
{
line 1;
line 2;
line 3;

example() //calling the same function

line 4;
line 5;

}
}

}
-------------------
My question is if the code looks like that,
when do line 4 and 5 get executed??
I am pretty sure line 4 and 5 get executed because if i remove them, the programme fails..

help me plz
Never does until you add a *GASP* Goto statement or another function with line 4 and 5 OR you have a switch statement.
Never does until you add a *GASP* Goto statement or another function with line 4 and 5 OR you have a switch statement.


*GASP* Incorrect.

Lines 4 and 5 are executed when the function returns.

Consider a simpler example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

void indent(unsigned level)
{
    std::cout << "Entered, level: " << level << '\n' ;

    if ( level )
        indent(level-1) ;

    std::cout << "Exiting, level: " << level << '\n' ;
}

int main()
{
    indent(4) ;
}


If you run this, line 10 is clearly being executed for every invocation of indent. Why? Because indent returns.
Last edited on
Topic archived. No new replies allowed.