For each item block not right

Fore each item loop function are not only looping within it self but also the whole if else section. I thought it worked as a do while loop, contained within it self.

You need to show your relevant blocks...and expected output

My bad it is working fine, copy and paste issue.