Informal example 1
while(some ice cream left)
eat a spoonful;
Informal example 2
while(not full)
{
eat ice cream;
get some more ice cream;
}
Example from text [ 02exalg.cpp ]
Semantics
while(A) Bexecutes the statements like this
A; B; A; B; ... until !A
for( int i=1; i<=10; i++) s=s+i;
Informal example
for( number_of_bottles=99; number_of bottles > 0; number_of_bottles--)
drink bottle of beer;
Count the semicolons.
Semantics
for(A; B; C) Dexecutes the statements like this
A; B; D; C; B; D; C; B; D; C; B; D; C; ... until !BIt does precisely the same thing as
{ A;
while(B)
{
D
C;
}
}
The do loop
do A while (B);executes
A; B; A; B; ... until !Aand is the smae as
A;
while(B)
A
for(bool p = false; p != true; p=!p)does not work! (Think about it).
So in the rare cases when we need to do this try
for(int count=0; count <=1; count++)
{ bool p = (count == 1);
...
}
Example of developing a program with a loop
[ E_(mathematical_constant) ]
Example of a quizlike While loop [ while0.cpp ]