Syntax

C++ Range-Based For Loop Syntax

In C++, range-based for loop: loops over every element — for (const auto& x : v) { ... }

Quick Answer

Syntax
for (const auto& x : v) { ... }
C++Syntax

Range-Based For Loop

Code example
for (const auto& x : v) { ... }

Loops over every element.

Learn More

How to range-based for loop in C++

  1. Open your C++ file or editor.
  2. Copy the example, then replace its sample names and values for your code.

C++ Range-Based For Loop Syntax

Use for (const auto& x : v) { ... }.

Related Syntax References

Frequently asked questions

What is the C++ syntax for range-based for loop?

Use this pattern: for (const auto& x : v) { ... }

Can I copy this C++ code example?

Yes. Use Copy code and adapt the example to your program.

Is this C++ syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify