https://rentry.org/PPP2_p112b

// Code derived from Stroustrup's PPP2 book
// § 4.4.2.3 for-statements
//  -and beginning on p 112

#include <iostream>

using std::cout;

// return the square of x
int square(int x) { return x * x; }

int main()
{
  for (int i = 0; i < 100; ++i) {  // for i in the [0:100) range
    cout << i << '\t' << square(i) << '\n';

    ++i;  // what's going on here? It smells like an error!
  }
}

build & run:

g++ -std=c++20 -O2 -Wall -pedantic ./ch_04/main_p112b.cpp && ./a.out

PrevUpNext

Edit
Pub: 01 Feb 2023 14:18 UTC
Edit: 29 Apr 2023 08:24 UTC
Views: 607