Skip to Main Content
C++ In a Nutshell
book

C++ In a Nutshell

by Ray Lischner
May 2003
Intermediate to advanced content levelIntermediate to advanced
808 pages
32h 24m
English
O'Reilly Media, Inc.
Content preview from C++ In a Nutshell

Name

for statement — For loop statement

Synopsis

               statement := for ( for-init-stmt [condition] ; [expression] ) statement
     for-init-stmt ::= expression-stmt | simple-decl
     condition ::= expression | type-specifier-seq 
               declarator = assignment-expr
            

The for loop is used for bounded loops and for unbounded loops that have well-defined iterations. Execution starts with for-init-stmt, which can be an expression statement or a declaration. (Note that the syntax for expression-stmt and simple-decl both include a terminating semicolon.)

condition is then evaluated. If condition evaluates to true, statement is executed. The iteration expression is then evaluated, and the condition is tested again. When condition is false, the loop ends and control passes to the statement following the end of the for statement.

Declarations in for-int-stmt and condition are in the same scope as expression and statement. A continue statement inside statement transfers control to the evaluation of the iteration expression.

Example

for (int i = 0; i < 10; ++i)
  cout << i << '\n';
for (node* n = head; n != 0 ; n = n->next)
  link_node(n);

See Also

break, continue, do, expression, statement, while, Chapter 4

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C++ High Performance

C++ High Performance

Viktor Sehr, Björn Andrist
Optimized C++

Optimized C++

Kurt Guntheroth
Mastering C++ Programming

Mastering C++ Programming

Jeganathan Swaminathan

Publisher Resources

ISBN: 059600298XSupplemental ContentErrata Page