When you need to repeat a task multiple times, you can use a loop instead of adding the same code over and over again.
Using a break
within the loop can stop the loop execution.
Loop through a block of code a specific number of times.
<?php
for($index = 0; $index < 5; $index ++)
{
echo "Current loop counter ".$index.".\n";
}
?>
/*
Output:
Current loop counter 0.
Current loop counter 1.
Current loop counter 2.
Current loop counter 3.
Current loop counter 4.
*/
Loop through a block of code if a condition is true.
<?php
$index = 10;
while ($index >= 0)
{
echo "The index is ".$index.".\n";
$index--;
}
?>
/*
Output:
The index is 10.
The index is 9.
The index is 8.
The index is 7.
The index is 6.
The index is 5.
The index is 4.
The index is 3.
The index is 2.
The index is 1.
The index is 0.
*/
Loop through a block of code once and continue to loop if the condition is true.
<?php
$index = 3;
do
{
// execute this at least 1 time
echo "Index: ".$index.".\n";
$index --;
}
while ($index > 0);
?>
/*
Output:
Index: 3.
Index: 2.
Index: 1.
*/
Loop through a block of code for each value within an array.
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?