A Complete Guide to Programming in C++ part 13. This book provides both novice and experienced programmers with a comprehensive resource manual for the C++ programming language. Readers gain experience in all aspects of programming, from elementary language concepts to professional software development, with in depth coverage of all the language elements en route. These elements are carefully ordered to help the reader create useful programs every step of the way. | THE FOR STATEMENT Initializing and Reinitializing A typical loop uses a counter that is initialized tested by the controlling expression and reinitialized at the end of the loop. Example int count 1 Initialization while count 10 Controlling cout count expression . loop endl count Reinitialization In the case of a for statement the elements that control the loop can be found in the loop header. The above example can also be expressed as a for loop Example int count for count 1 count 10 count cout count . loop endl Any expression can be used to initialize and reinitialize the loop. Thus a for loop has the following form Syntax for expression1 expression2 expressions statement expressionl is executed first and only once to initialize the loop. expression2 is the controlling expression which is always evaluated prior to executing the loop body if expression2 is false the loop is terminated if expression2 is true the loop body is executed. Subsequently the loop is reinitialized by executing expressions and expression2 is re-tested. You can also define the loop counter in expressionl. Doing so means that the counter can be used within the loop but not after leaving the loop. Example for int i 0 i 10 cout i As this example illustrates the loop body can be an empty statement. This is always the case if the loop header contains all necessary statements. However to improve readability even the empty statement should occupy a line of its own. 100 CHAPTER 6 CONTROL FLOW THE for STATEMENT CONTINUED Sample program Outputs a table of exchange Euro and US- include iostream include iomanip using namespace std int main long euro maxEuro Amount in Euros double rate Exchange rate Euro - cout n TABLE OF EXCHANGE Euro - US- n n cout nPlease give the rate of exchange one Euro in US- cin rate cout nPlease enter the maximum euro cin maxEuro --- Outputs the table --- Titles of columns cout n setw 12 Euro setw 20 US- t tRate rate endl Formatting US- cout fixed setprecision 2 .