Handling nested for loop case