|
A while loop will have all the components that a
for loop had, except they will be in different location. Open
rep05a.cpp and run it.
int
main()
{
int i = 0;
while (i < 4)
{
cout << "i: " << i << endl;
i++;
}
return 0;
}
This example is just to show you all the parts of the while loop. The
first part is the
initialization:
int i = 0; The second part is the
conditional:
i < 4 The third part is the
increment:
i++.
The while loop will execute everything in between
the curly braces as long as the conditional is true.
The while is called a pre-conditional
because the test is done first before any part of the loop
executes. If the condition is false at the start, the loop
never runs. Change the above code to
int i = 8. If you run
it, there will be no output because i < 4 is false and the loop does
not run. |