Tech Blog   »   Home   »   Coders Forum   »   Computer Directory   »   Math Calculators   »   RSS:Directory|Forum 

Programs - Source Code C++ for loops

To copy code, highlight with mouse, copy, and paste.



Loops: for loops
// - Show all odd numbers using a for loop
#include <iostream>
using namespace std;
int main()
{
	for (int count = 1; count <= 41; count += 2)
	{
		cout << count << ", ";
	}
	cout << endl;
	return 0;
}
/*==================[output]=================================================
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41,
Press any key to continue . . .
=============================================================================*/



// - Count backwards using a for loop.
#include <iostream>
using namespace std;
int main()
{
	for (int count = 15; count > -1; count--)
	{
		cout << count << ", ";
	}
	cout << endl;
	return 0;
}
/*=================[output]==========================
15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
Press any key to continue . . .
=====================================================*/



// - Count by fives using a for loop.
#include <iostream>
using namespace std;
int main()
{
	for (int count = 0; count <= 100; count += 5)
	{
		cout << count << ", ";
	}
	cout << endl;
	return 0;
}
/*=====================[output]==================================================
0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100,
Press any key to continue . . .
=================================================================================*/

C++ Links


Code Examples:
Variables
Strings
Arrays

Functions:
Return Values
Pass by Value
Pass by Reference

Loops:
While loop
Do-while
For loops

Arrays:
Basic Array






More C++ source code

Forum Area maSpinner

The forum contains more programming examples, registration is free!





Validated with no errors:

Valid XHTML 1.0 Transitional     Valid CSS!


Site Map