ITERATIVE PROCESSING


Loops let you repeat a certain section of code until a given condition is met. They save programmers time when creating code. Loops, for instance, can process hundreds or even thousands of elements of data using only a few programming statements. Loops are especially useful for processing the contents of arrays and collections, such as the collection associated with the ComboBox control. Loops can also drive any form of iterative logic, such as running a few lines of code that prompt a user for information. In such a case, you can use a loop to direct that the code continue to run until the appropriate information is provided.

Hint 

A loop is a group of programming statements that are executed repeatedly.

Visual C++ gives you several options when it comes to establishing a loop:

  • for. Creates a loop that iterates a specific number of times

  • while. Creates a loop that iterates as long as a condition remains true

  • do...while. Creates a loop that is guaranteed to iterate at least once and will continue to do so as long as a condition remains true

Each type of loop is discussed in greater detail in the sections that follow.

while Loops

You can set up while loops to iterate until or while a specific condition is true. These types of loops are well suited when you know you want to loop repeatedly as long as some condition is true. For example, you might set up a do...while or while loop to allow a user to enter as much input as desired, terminating the loop's execution only when the user enters a trigger word such as Quit or Exit.

while

The while loop acts as a combination if statement and loop. It is designed to test a condition and then execute provided the condition proves to be true. It has the following syntax:

 while( condition )   statement; 

Condition is expressed in the form of an expression, as demonstrated here:

 Windows::Forms::DialogResult dlResult; Int32 intCounter = 0; const Int32 cintMaxValue = 5; while( intCounter < cintMaxValue ) {   dlResult = MessageBox::Show("Do you wish to increase the counter?",               "Increase Counter", MessageBoxButtons::YesNo,               MessageBoxIcon::Question,               MessageBoxDefaultButton::Button1 );   if( dlResult == Windows::Forms::DialogResult::Yes )     intCounter++; } 

In this example, a loop is established that continues running the code within it while its expression evaluates to true. First, the variables dlResult, intCounter, and cintMaxValue are declared, and intCounter and cintMaxValue are initialized. Then program flow transfers to the loop. The loop tests the condition intCounter < cintMaxValue. If this expression proves to be true, the code within the loop executes. If it proves to be false, the entire body of the loop (enclosed in braces) is skipped.

Within the loop, a dialog box appears asking the user whether he wants to update intCounter. The dialog box has only two buttons, giving the user a Yes or No response. After the user responds to the dialog box, the result is stored in dlResult. Below this, the if statement tests the results of the user's response to the dialog box. If the result of the query is No, intCounter is not updated. If the user response to the dialog box is Yes, intCounter is updated by one. Program control continues past the if statement to the closing braces, where it jumps back to the while statement. The condition is then tested again.

The user must respond Yes five times to the dialog box query to get the loop to exit. Responding Yes causes intCounter to be incremented from 0 to 5, whereupon the logical test intCounter < cintMaxValue evaluates to false (because 5 < 5 is logically false). Program control then skips the entire body of the loop and continues executing whatever code lies beneath it.

In this example, the dialog box displays an indeterminate number of times as long as the user continues to click No. The example illustrates how flexible the whi1e loop is. This loop allows your application to execute code as long as is necessary until some condition is reached.

dowhile

A do...white loop is a variant of the while loop, except that it is reversed. You can think of it as a while loop turned upside down. Like the while loop, it iterates as long as a specified condition remains true. However, since there is no test at the top of the loop, the code within the loop is guaranteed to execute at least once. The syntax is shown here:

 do {   statements   .   .   . } while( condition ); 

As with if statements, condition represents an expression that is tested upon each iteration of the loop. The loop continues executing as long as the condition is true. Here is an example:

 Int32 intCounter = 0; do {   intCounter += 1; } while( intCounter < 10 ); 

In this example, the variable named intCounter is declared and then used to control the iteration of the loop. When an application first encounters this code, the intCounter is incremented by one. The while statement then checks to see if intCounter is less than 10. If it is, program flow jumps back to the top of the loop, starting with the do keyword. The result in this example is that the value of IntCounter is increased by 1 at the end of each iteration, causing the loop to iterate 10 times.

A loop will continue executing so long as its controlling statement proves true. After the condition being tested by the while statement proves false, the loop ends automatically. In the previous example, this occurred when intCounter reached a value of 10. The while statement would have evaluated 10 < 10 logically as false.causing program flow to continue executing past the loop.

for Loops

The for loop is an excellent choice when you know in advance how many times you want a loop to execute. The for loop iterates a specific number of times as determined by a counter, which can increase or decrease based on the logic you are implementing.

The syntax for the for statement is shown here:

 for( initial expression; condition; loop expression )    statement; 

The for statement executes a given statement until condition becomes false.statement can be a single line of code or a block enclosed in braces. The most typical use of a for statement involves setting a counter to a value, testing for a condition, and then altering the counter at the end of each iteration. Consider the following example:

 private: System::Void Form1_Load(System::Object^   sender, \ System::EventArgs^  e) {   Int32 intArray[100];   for( Int32 intCntr = 0; intCntr < 100; intCntr += 1 )      intArray[intCntr] = intCntr; } 

In this example, an array intArray is first declared with 100 elements. A for loop is established to iterate through it. The variable controlling the loop, intCntr, is first initialized to 0.

Next, the condition of the loop, intCntr < 100, is tested. The loop then executes the statement beneath it, intArray[intCntr] = intCntr. Finally, the loop expression, intCntr += 1, activates. The result is that all 100 elements of the loop are filled with values ranging from 0 to 99. When intCnter is finally incremented to 100, the conditional expression fails and the loop ends, transferring program control to the statements following the loop.




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