Add to Favorites

programming terminology - loop

A loop ( in programming ) is much like a loop in real life, it is a procedure that runs multiple times in a row. A loop usually has a condition, that will say whether or not the loop will continue or end. This condition could be a number range.

For example, I have a procedure that sums up a total from a list of items, I will loop over the list items and add them together. My loop will be conditioned to end once it reaches the end of the list.

Loops can be simple like the above loop, but they can also become quite complex such as when multiple loops are nested within each other. In programming, the loop is an indispensable tool.

An Example of a loop in php

[code]

$our_list = array(1,5,7,8,9);
$total = 0;

foreach($our_list as $item_to_sum) {
$total = $total + $item_to_sum;
}

echo "Your total is $total";

[/code]

Another example, this will say hello to you many times

[code]

for($i = 0; $i < 10; $i++) {
echo "Hello you";
}

[/code]

This is how the common loop looks, you have a variable where you are storing a value, you have a condition that will evaluate whether or not the loop continues, then you have an action to be taken to modify the variable each time the loop is run. In this loop, you see that we set the variable $i to zero, we will run the loop as long as the variable $i is less than 10, and each time we run the loop we will add one to $i.

Comments

Be the first to leave a comment on this post.

Leave a comment

To leave a comment, please log in / sign up