Table of contents | |
Introduction | |
Syntax of For Loops | |
Flowchart of For Loops | |
Examples of For Loops | |
Conclusion |
For Loops are a type of loop in C++ that allows a specific block of code to be executed repeatedly until a certain condition is met. It is especially useful when you know how many times the loop should run.
The For Loop is the most commonly used loop in C++, and is used to iterate through arrays, strings, and other collections of data. In this article, we will go over the syntax, flowchart, and examples of the For Loop.
The syntax of the For Loop is as follows:
for (initialization; condition; increment/decrement)
{
// statement(s)
}
Here's a breakdown of each component of the For Loop:
A flowchart is a graphical representation of the For Loop that shows the flow of control through the loop. Here's an example of a flowchart for a basic For Loop:
Example 1: Basic For Loop
Here's an example of a basic For Loop that prints the numbers 1 to 5:
#include <iostream>
int main()
{
for (int i = 1; i <= 5; i++)
{
std::cout << i << std::endl;
}
return 0;
}
1
2
3
4
5
In this example, the loop control variable i is initialized to 1. The condition checks whether i is less than or equal to 5. If the condition is true, the loop executes and prints the value of i. The loop control variable is then incremented by 1, and the condition is checked again. This process continues until i is no longer less than or equal to 5.
Example 2: For Loop with Break Statement
Here's an example of a For Loop that uses a break statement to exit the loop:
#include <iostream>
int main()
{
for (int i = 1; i <= 10; i++)
{
std::cout << i << std::endl;
if (i == 5)
{
break;
}
}
return 0;
}
1
2
3
4
5
In this example, the loop control variable i is initialized to 1. The condition checks whether i is less than or equal to 10. If the condition is true, the loop executes and prints the value of i. The loop control variable is then incremented by 1, and the condition is checked again. However, there is a break statement that is executed when i is equal to 5. This causes the loop to exit before i reaches 10.
Example 3: For Loop with Nested Loops
Here's an example of a For Loop with nested loops that prints the multiplication table for numbers 1 to 5:
#include <iostream>
int main()
{
for (int i = 1; i <= 5; i++)
{
std::cout << "Multiplication table for " << i << std::endl;
for (int j = 1; j <= 10; j++)
{
std::cout << i << " x " << j << " = " << i * j << std::endl;
}
std::cout << std::endl;
}
return 0;
}
Multiplication table for 1
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
1 x 8 = 8
1 x 9 = 9
1 x 10 = 10
Multiplication table for 2
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
Multiplication table for 3
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
Multiplication table for 4
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40
Multiplication table for 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
In this example, the outer loop controls the multiplication table that is being printed. The inner loop prints the multiplication table for each number, from 1 to 10.
The For Loop is a useful and powerful construct in C++. It allows you to iterate over a set of data, executing a specific block of code each time. Understanding the syntax and flowchart of the For Loop is essential to using it effectively in your programs.
70 videos|45 docs|15 tests
|
|
Explore Courses for Software Development exam
|