C++ For Loop with EXAMPLE

What is a For Loop?

This is a repetition control structure that helps us iterate over a section of C++ code for a fixed number of times. A for loop runs provided the test expression is true. The loop terminates execution immediately the test expression becomes false. This means before the execution of the loop body in each iteration, the condition has to be evaluated. If the evaluation returns a true, the loop body is executed. If the evaluation returns a false, execution of the loop body is terminated.

In this C++ tutorial, you will learn:

How for loop works?

The for loop works as follows:

Flow Chart Explanation:

  1. The C++ language compiler begins by evaluating the initialization. This is only done once as execution begins.
  2. The test expression is evaluated/executed.
  3. If the test expression is true, the loop body is executed and the test expression is updated. If expression becomes false, the for loop terminates.
  4. After the execution of test expression, the increment is executed to increase the value of the loop control variable.
  5. The test expression is evaluated again, and the process continues until the expression becomes false.
  6. If the exoression is false, the loop body statements are skipped.

Note: The test expression is updated after every iteration. This means different values of the loop control variable are executed in each iteration.

When to use a for loop?

The for loop should be used when:

Syntax of for loop

Here is the syntax for the for loop:

for ( initialization;condition;increment ) {
   statement(s);
}

Here is an explanation of the above parameters:

Example 1

#include <iostream>
using namespace std;
int main() {
	for (int x=0; x<5; x=x+1) {
		cout << "X is: " << x << endl;
	}
	return 0;
}

Output:

Here is a screenshot of the code:

Code Explanation:

  1. Including the iostream header file in our code. It will allow us to read from and write to the console.
  2. Including the std namespace so as to use its classes and functions without calling it.
  3. Calling the main() function inside which the logic of the program should be added. The { marks start of body of the main() function.
  4. Creating a for loop. The initialization creates an integer variable x and assigns it a value of 0. The condition states that the value of x must be less than 5. The increment increases the value of x by 1 after every iteration. The { marks the beginning of the body of the for loop.
  5. To print the value of variable x alongside other text on the console. The endl is a C++ keyword meaning end line. The cursor will print in the next line in the next iteration.
  6. End of the loop body.
  7. The main() function should return an value if the program runs fine.
  8. End of the body of the main() function.

Example 2

#include <iostream>
using namespace std;
int main()
{
	int x, num, factorial = 1;
	cout << "Type positive number: ";
	cin >> num;
	for (x = 1; x <= num; ++x) {
		factorial *= x;   // factorial = factorial * x;
	}
	cout << "Factorial of " << num << " = " << factorial;
	return 0;
}

Output:

Here is a screenshot of the code:

Code Explanation:

  1. Including the iostream header file in our code. It will allow us to read from and write to the console.
  2. Including the std namespace so as to use its classes and functions without calling it.
  3. Calling the main() function inside which the logic of the program should be added.
  4. The { marks start of body of the main() function.
  5. Declaring integer variables, x, num, and factorial. The variable factorial has been assigned a value of 1.
  6. Printing some text on the console.
  7. Prompting user to enter a value for variable num.
  8. Creating a for loop. The initialization creates an integer variable x and assigns it a value of 1. The condition states that the value of x must be less than or equal to value of variable num. The increment increases the value of x by 1 after every iteration. The { marks the beginning of the body of the for loop.
  9. Calculating the value of factorial using the formula factorial = factorial * x.
  10. End of the loop body.
  11. To print the value of variables num and factorial alongside other text on the console.
  12. The main() function should return an value if the program runs fine.
  13. End of the body of the main() function.

Summary

 

YOU MIGHT LIKE: