Iterative structure: while
This structure allows the execution of a set of actions iterativelly while a given condition remains true. That condition must result in a logic (bool) value. In order to avoid infinite loops, the structure's actions must include some action that modifies the condition's value (or use the statement break). The condition is evaluated at the beginning of the structure, so if it turns to be false the first time, the actions inside the structure will not be executed.
Example: Finding a number (n) in an array with 8 elementos:
`int i=0, array[8]={2,5,3,7,8,5,1,0};
cout<<"Enter the number to search:";
cin>>n;
while (array[i]!=n && i<8) {
i++;
}
if (i==8)
cout<<"Not found."<<endl;
else
cout<<"Found in position "<<i<<endl;
`
Variable i is used to traverese the array. The loop content (to increment i) is runned while i doesn't hold the position of the number y while there are more elements to traverse. The program exits the loop when the condition becomes false. As it is a compound condition, it can happend in two situations: the number was found (so firs part, arreglo[i]!=n, is false), or all the elements where traverse (so second part, i<n, is false).