1 О циклах
Ivor Barhansky edited this page 2020-10-26 12:10:31 +00:00

Always use FOREACH when iterating over a whole array, or at least from the top of an array to a BREAK condition. This way you will never be bitten by an "off by one" error.

Always use FOR when you know (or can easily determine) exactly how many times you need to execute the code in the loop. This is also useful for iterating parts of an array where you aren't always going to be starting from the head.

Use a DO-WHILE when the number of times to have to execute the loop is indeterminate, but you know the code must execute at least once.

Use a WHILE loop when the looping condition could be false as you enter the loop. In other words, there could be conditions where you never execute the loop.