The for statement or for loop is considered as a predefined loop because the number of times it iterates to perform its body is predetermined in the loop's definition.
The for loop contains a counter whose values determine the number of times the loop iterates. The iteration stops upon reaching the number of times specified in the loop.
The general form of the for statement is:
for (initialization; condition, increment) {
statement sequence;
}
where:
initialization is an assignment statement that is used to set the loop's counter
condition is a relational Boolean expression that determines when the loop will exit
increment defines how the loop's counter will change each time the loop is repeated
statement sequence may either be a single Turbo C statement or a block of Turbo c statements that make up the loop body
The for loop continues to execute until the condition is TRUE. Once FALSE, program execution resume on the statement following the for loop.
Note:
Never place a semi-colon right after the for header. This is a logical error.
Never change the value of the for loop's counter inside the body of the loop. This will affect the results of the program.
The increment part of the for loop is execute after the first iteration of the loop
Example 1:
/* Write a program that will print “HELLO” five times. */
#include <iostream>
using namespace std;
int x;
int main() {
for (x=1; x<=5; x++) {
cout<< "HELLO\n";
}
return 0;
}
SAMPLE OUTPUT:
HELLO
HELLO
HELLO
HELLO
HELLO
Example 2:
/* Write a program that will print the numbers 1 to 10. */
#include <iostream>
using namespace std;
int x;
int main() {
for (x=1; x<=10; x++) {
cout<<x<<" ";
}
return 0;
}
SAMPLE OUTPUT:
1 2 3 4 5 6 7 8 9 10
In the above example, the counter of the for loop is x with the initial value of 1. After the initialization part, the control of the program transfer to the condition to check whether the value of less than or equal to 10. Since the result of the condition is TRUE the value of x will be printed.
After the first iteration of the loop, the increment part will be performed making the value of x equal to 2. The control of the program transfers again to the condition part to determine if the result is TRUE or FALSE. Since condition is TRUE the value of x will again be printed then the increment part will be performed again.
The process continues until the condition part evaluates to FALSE.
Example 3:
/*Write a program that will output the sum of 1 to 10 */
#include <iostream>
using namespace std:
int num;
int sum;
int main() {
for (num=1; num=10; num++) {
sum-sum + num:
}
cout<< "\nThe Sum of 1 to 10 is "c< sum:
return 0;
}
SAMPLE OUTPUT
The sum of 1 to 10 is 55
In the nested for loop, the inner loop is completed first before returning to the main loop and testing the condition again.
The general form of the nested for loop statement is:
for (initialization; condition; increment 1) {
for (initialization;condition; increment 2) {
statement (s);
}
}
Example 1:
// Write a program that will create a 10 x 10 multiplication table
# include <iostream>
using namespace std;
int row, column, table;
int main() {
for (column=1;column<=10;column) {
for (row=l;row<=10; row++) {
table= row*column;
cout<<table <<" \t";
}
}
return 0;
}
SAMPLE OUTPUT:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 56 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100