#include <iostream> using namespace std; int main() { int sum = 0; int fact = 1; for(int i = 1; i <= 5; i++) { sum += i; // i is known throughout the loop fact *= i; } // but, i is not known here. cout << "Sum is " << sum << "\n"; cout << "Factorial is " << fact; return 0; }
Sum is 15 Factorial is 120"
3.15.for loop | ||||
3.15.1. | Simplest for loop statement | |||
3.15.2. | A conversion table of feet to meters | |||
3.15.3. | Use multiple statements in for loops | |||
3.15.4. | Display the alphabet: use char to control for loop | |||
3.15.5. | A negatively running for loop | |||
3.15.6. | Loop until a random number that is greater than 20,000. | |||
3.15.7. | A for loop with no increment | |||
3.15.8. | The body of a for loop can be empty | |||
3.15.9. | Declare loop control variable inside the for | |||
3.15.10. | Use nested for loops to find factors of numbers between 2 and 100 | |||
3.15.11. | For loops with null statements | |||
3.15.12. | empty for loop statement | |||
3.15.13. | Compound interest calculations with for | |||
3.15.14. | Floating point control in a for loop | |||
3.15.15. | Generating multiplication tables |