table looping logic