Controlling a for Loop with Floating-Point Values - C++ Statement

C++ examples for Statement:for

Description

Controlling a for Loop with Floating-Point Values

Demo Code

#include <iostream>
#include <iomanip>

int main()/*from   ww  w .  ja  va 2  s .  co  m*/
{
  const double pi { 3.14159265 };     // The famous pi
  const int perline {3};           // Outputs per line
  int linecount {};                // Count of output lines
  for (double radius {0.2} ; radius <= 3.0 ; radius += 0.2)
  {
    std::cout << std::fixed << std::setprecision(2) << " radius =" << std::setw(5)
              << radius << "  area =" << std::setw(6) << pi * radius * radius;
    if (perline == ++linecount)       // When perline outputs have been written...
    {
      std::cout << std::endl;         // ...start a new line...
      linecount = 0;                  // ...and reset the line counter
    }
  }
  std::cout << std::endl;
}

Result


Related Tutorials