C++ Loops

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

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?