Submitted by dellagd on April 8, 2009 - 8:41pm.
in this branch of my C++ series, I will show how to use while functions. A while function will be a loop until its parameter ( like the (b < 5) part of a if statement) evaluates false, in which it will exit the while loop and continue with the code. Example:
int b = 1;
while (b <= 10)
{
std::cout << " b is less than 10 ";
b++;
}
std::cout << "b is now greater than 10";
Analysis:
line 1: declares and sets b to 1 in one shot
line 4: declares that there will be a while statement following and the parameter is "b is less than or equal to 10"
line 6: displays " b is less than 10 "
line 7: increments b by 1. So every time the program sees b++, it will add 1 to b (only in C++, not C. For c you will have to use "int temp; temp = b + 1; b = temp;"
line 9: once b is 11 or greater, it displays "b is now greater than 10"
This statement can also be very useful if you want to have a infinite loop:
while (1)
{}
or
while (1 == 1)
{}
check back for more C++ tutorials.