1 2 3 4
|
while ( <condition> )
{
...
}
|
The condition can either be true or false. While it's
TRUE the loop keeps going on.
1 2 3 4 5 6
|
int i = 0;
while( i < 10)
{
cout << "Hello" << endl;
i++;
}
|
This will print
Hello ten times.
i = 0 -> Less than 10? -> YES -> Execute ( print the string and INCREASE the variable i )
i = 1 -> Less than 10? -> YES -> Execute ( print the string and INCREASE the variable i )
i = 2 -> Less than 10? -> YES -> Execute ( print the string and INCREASE the variable i )
i = 3 -> Less than 10? -> YES -> Execute ( print the string and INCREASE the variable i )
i = 4 -> Less than 10? -> YES -> Execute ( print the string and INCREASE the variable i )
i = 5 -> Less than 10? -> YES -> Execute ( print the string and INCREASE the variable i )
i = 6 -> Less than 10? -> YES -> Execute ( print the string and INCREASE the variable i )
i = 7 -> Less than 10? -> YES -> Execute ( print the string and INCREASE the variable i )
i = 8 -> Less than 10? -> YES -> Execute ( print the string and INCREASE the variable i )
i = 9 -> Less than 10? -> YES -> Execute ( print the string and INCREASE the variable i )
i = 10 -> Less than 10? ->
NO ->
EXIT