C++ Loops

30% OFF - 9th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY9

Introduction

A loop statement executes a group of statements repeatedly until a condition is met. There are 3 types of primitive loops in C++: for, while, and do...while.

Syntax

  • while (condition) statement ;
  • do statement while (expression) ;
  • for (for-init-statement ; condition ; expression) statement ;
  • for (for-range-declaration : for-range-initializer) statement ;
  • break ;
  • continue ;

Remarks

algorithm calls are generally preferable to hand-written loops.

If you want something an algorithm already does (or something very similar), the algorithm call is clearer, often more efficient and less error prone.

If you need a loop that does something fairly simple (but would require a confusing tangle of binders and adapters if you were using an algorithm), then just write the loop.



Got any C++ Question?