for is a keyword to start a for-loop.
For loop syntax
for ( /* initialization */ ; /* breaking condition */ ; /* after-loop operation */ )
{
// The code block that will be repeated while the breaking condition is true
}
- Prefer algorithms over loops [1,2] (see Exercise #9: No for-loops to learn how to do so)
- Prefer a for-statement to a while-statement when there is an obvious loop variable [5]
- Prefer a while-statement to a for-statement when there is no obvious loop variable [6]
- Since C++11, prefer a range-for-statement to a for-statement when there is a choice [4]
- [1] Bjarne Stroustrup. The C++ Programming Language (3rd edition). 1997. ISBN: 0-201-88954-4. Chapter 18.12.1 : 'Prefer algorithms over loops'
- [2] Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. 2005. ISBN: 0-32-111358-6. Item 84: 'Prefer algorithm calls to handwritten loops'
- [3] GCC page about C++0x support
- [4] Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 9.8. Advice. page 240: '[3] Prefer a range-for-statement to a for-statement when there is a choice'
- [5] Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 9.8. Advice. page 240: '[4] Prefer a for-statement to a while-statement when there is an obvious loop variable'
- [6] Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 9.8. Advice. page 240: '[5] Prefer a while-statement to a for-statement when there is no obvious loop variable'