Before we talk about
What a foreach loop does is take in data from an array, then execute some code for each item in the array.
The code for an foreach loop looks like this:
A standard foreach loop will loop as many times as there are items in the array. If there are 10 products, it will execute 10 times. On pages with pagination (category, search, product list, etc.), there is a field in the admin where you can set the limit on each page. If you have 10 products and limit the loop to 8 per page, a button will appear at the bottom that says "Previous." On that page, you will be able to see the other two products.
In the above sample code you'll notice there is a parameter of "iterator" and one for "array". These change depending on what type of data you are looping through.
The above loop isn't very helpful, normally we'd be writing entities to the page, add to cart buttons, etc. to make this page more useful to the user. A real site might have a foreach loop that looks like this:
The code above retrieves the array for products in the category, then for each product, it will run the code inside of it. So if we have 10 products on our page, the code will run 10 times.
You can get even more advanced control by combining foreach loops with nested if statements.