codegurukool.com · Free coding tutorials & online compiler

Home › C++ Tutorial › C++ for loop

C++ for loop

Counting and ranges.

Classic for counts. The range-based for (auto x : items) walks a collection.

Example code

#include <iostream>
using namespace std;
int main() {
  for (int i = 0; i < 3; i++) cout << i << " ";
  int a[] = {10, 20, 30};
  for (int x : a) cout << x << " ";
  cout << endl;
  return 0;
}

Run this code in the online editor

← C++ while loopC++ break and continue →