Skip to content

Instantly share code, notes, and snippets.

@juanfal
Created October 10, 2012 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanfal/3865150 to your computer and use it in GitHub Desktop.
Save juanfal/3865150 to your computer and use it in GitHub Desktop.
for-loop sample. Summing up numbers
// 03-12.forloopsum.cpp
#include <iostream>
using namespace std;
const int TOP = 1000;
int main()
{
int sum = 0;
for (int n = 1; n <= TOP; n++) // Note that the variable n is a local
sum = sum + n; // variable of the body of the for loop!
cout << "The sum of the numbers 1 to " << TOP << " is "
<< sum << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment