May 2002
Beginner
320 pages
6h 18m
English
In many cases, you want to do something at least once and perhaps many times. This is best handled by the do/while loop.
The form of this loop is
do
{
statements
}
while (condition);
Let's have a look at its use in the main() function of the division calculator (see Listing 9.1).
1: int main(int argc, char* argv[]) 2: { 3: SAMSErrorHandling::Initialize(); 4: *5: do // At least once... *6: { 7: try 8: { 9: float Dividend = GetDividend(); 10: float Divisor = GetDivisor(); 11: 12: cout << Divide(Dividend,Divisor) << endl; 13: } 14: catch (...) 15: { 16: SAMSErrorHandling::HandleNotANumberError(); 17: } ; *18: } *19: while (SAMSPrompt::UserWantsToContinue ("More division? ")); 20: ... |
Read now
Unlock full access