Problem in the while loop

I am not able to understand the while loop used here:-

if (i == -1)
{
if (a[0] == key)
return 0;
else return -1;
}

hello @Rohit39 please share the complete code so that i can exlain in the better way.
i need to see what is the condition in the while loop and all.

int last_occurence_search(int *a, int n, int key) { if (n == 0) return -1; int i = last_occurence_search(a + 1, n - 1, key); if (i == -1) { if (a[0] == key) return 0; else return -1; } return i + 1; }

hey @Rohit39where is the while loop in this code?

sorry my bad…it is the if-else condtiion

See after all the recursion at last n will become 0 so then -1 will be stored in i then there is one they are checking if iis -1 then you will check for the 0th element at that point if it is k then update i otherwise return-1 only.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.