Akash Bera
Akash Bera

Reputation: 53

Terminating condition of inner loop is same as in outer loop

void preorder(tree *node)
{
   do
    {
           while(node != NULL)
             {
                 printf("%d  ", node -> data) ;
                  if(node -> right != NULL)
                  top = push( top , node -> right) ;
                  node = node -> left ;
             }
           if(top != NULL)
            {
              node = top -> ptr ;
              top = pop( top ) ;
            }
    }while(top != NULL || node != NULL) ;
}

In the above block of code, the terminating condition in inner while loop is the sub-part of the outer do-while loop. Then, will the outer loop terminate at the same time when the inner while loop is terminated?

Upvotes: 1

Views: 99

Answers (2)

C. L.
C. L.

Reputation: 571

After the inner loop has terminated, the outer one will be exited, if top is NULL (if block is not entered). Otherwise, it depends on the assignments in the if block.

So in your case, termination of the inner loop does not imply termination of the outer loop.

Upvotes: 0

Anmol
Anmol

Reputation: 123

No when the inner loop ends, it hands over the control to outer loop. The outer loop performs the remaining task in the current iteration and the starts the next iteration or terminates based on condition applied. The inner loop condition will not effect the outer loop.

Upvotes: 3

Related Questions