CONTROLLING EXECUTION WITHIN LOOPS


You might find it necessary to exit a loop early. Or you might find it useful to skip a particular sequence of statements within the loop. Visual C++ provides two easy-to-use commands for doing so that work with all types of loops.

Using break

You can use the break statement to break out of any loop. The syntax for this is shown in two example loops here:

 while( condition ) {   if( expression )     break; } for( condition ) {   if( expression )     break; } 

As you can see, these examples are quite similar. In the first, a while loop begins based on some condition. An if then evaluates some other expression, which, if true, executes the break command. When encountered, this command causes the loop to terminate immediately and transfer control to the code following the loop. The same sequence applies to the for loop example. This loop continues as long as the condition within its parentheses is true. When the break expression is encountered, control transfers to the closing brace and then to any statements, which follow:

 private: System::Void Form1_Load(System::Object^   sender,\ System::EventArgs^  e) {   Windows::Forms::DialogResult dlResult;   Int32 intCounter = 0;   const Int32 cintMaxValue = 1000;   while( intCounter < cintMaxValue )   {     intCounter++;     dlResult = MessageBox::Show("Do you wish to stop incrementing"               "the counter?", "Increase Counter",               MessageBoxButtons::YesNo,               MessageBoxIcon::Question,               MessageBoxDefaultButton::Button1 );     if( dlResult == Windows::Forms::DialogResult::Yes )       break;   } } 

In this example, when the application's form first loads, a counter is incremented from 0 to 1000. The variables are declared, and the loop checks whether intCounter is less than cintMaxValue. The user is then presented with a dialog box asking if he wants to stop increasing the counter. If the response is no, the loop continues. However, if the expression dlResult == Windows::Forms::DialogResult::Yes evaluates to true, the break command is called. This causes the loop to terminate immediately and the rest of the application to run.

Using continue

An alternative to the break statement, the continue statement allows you to stay within a loop but skip any statements below it. Paired with logical tests, you can use this to control precisely how a loop iterates. As with break, you can use continue in any type of loop. Here is the syntax using two different types of loops as examples:

 do {   if( expression )     continue; } while( condition ); for( condition ) {   if( expression )     continue; } 

The following example demonstrates how to use the continue statement to skip sections of a loop:

 private: System::Void Form1_Load(System::0bject^   sender, \ System::EventArgs^  e) {   Windows::Forms::DialogResult dlResult;   Int32 intCounter = 0;   const Int32 cintMaxValue = 1000;   while( intCounter < cintMaxValue )   {     intCounter++;     if( intCounter < 100 )       continue;     dlResult = MessageBox::Show("Do you wish to stop incrementing"               "the counter?", "Increase Counter",               MessageBoxButtons::YesNo,               MessageBoxIcon::Question,               MessageBoxDefaultButton::Button 1 );     if( dlResult == Windows::Forms::DialogResult::Yes )       break;   } } 

As in previous examples, this example iterates as long as intCounter is less than 1000. The difference here is the if statement test, which contains a test to determine whether the value of intCounter is less than 100. If this evaluates to true, the continue statement is executed. This effectively causes all statements within the loop to be skipped. Control of the program then returns to the top of the loop. The result is that for the first 100 iterations, none of the code is called that presents a dialog box and exits the loop if the user chooses.




Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner 2006
Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner 2006
ISBN: 735615381
EAN: N/A
Year: 2005
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net