Print all k ary string of length n

I

I am confused how loops inside this program is working please help me in that