https://rentry.org/PPP2_p109

// Code derived from Stroustrup's PPP2 book
// § 4.4.2.1 while-statements
//  -and beginning on p 109

#include <iostream>

using std::cout;

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

// calculate and print a table of squares 0–99
int main()
{
  int i = 0;  // start from 0

  while (i < 100) {  // the loop condition testing the loop variable i
    cout << i << '\t' << square(i) << '\n';

    ++i;  // increment i (that is, i becomes i+1)
  }
}

build & run:

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

PrevUpNext

Edit
Pub: 31 Jan 2023 17:37 UTC
Edit: 29 Apr 2023 08:15 UTC
Views: 402