If integrating loops into your programs is a struggle, know this: Loops can be broken down into at least two categories (possibly more):
- While something isn't yet complete
- Iterate through an array
I'll exemplify both here:
While something isn't yet complete: For example, say you had a variable that holds the current amount of seconds elapsed since the initial execution of the program, and you wanted to do something specific while the amount of seconds isn't at a certain value.
1 2 3 4
|
while(seconds_elapsed < 60)
{
// Invoke functions...
}
|
Iterate through an array: For example, say you had an array of
n integers, and you were looking for the value of
x. You would iterate through the array until you found it, like so:
1 2 3
|
for(unsigned int item_offs(0U); item_offs < 10U; ++item_offs)
if(my_array[item_offs] == 100)
break;
|
Each loop construct is good for something. It's your job, as a coder, to determine which loop is most suitable for the task in hand.
Wazzak