In this post, we will learn about the C++ for loop and its working with the help of examples.
Loops are used in computer programming to repeat a section of code.
- Let’s imagine we wish to broadcast a message 100 times. We may then use a loop instead of writing the print statement 100 times.
- That was only a simple example; loops can help us attain far greater efficiency and sophistication in our applications.
The C++ programming language, in general, has a sequential execution strategy. The compiler executes the first statement before moving on to the next statement in line. When using a conditional statement, the statement is performed based on the condition’s outcome. Furthermore, you may use looping instructions in C++ to repeat the execution of a certain block of code until a certain condition is met. You might want to print numbers from 1 to 10 as an example. Instead of manually creating 10 individual print statements, you can use a loop statement to have the compiler manually increment the variable and report the value after each iteration.
Where we used loops?
- Loops are used in computer programming to repeat a section of code. If you want to repeat a message 100 times, for example, you can use a loop instead of inputting the same code 100 times.
- The C++ for loop is used to repeatedly iterate a section of the program. If the number of iterations is fixed, the for loop is suggested.
The 3- Types of looping statements available in C++:
- For loop
- While loop
- Do .. While loop
For Loop
In C++ programming, a for loop can be used to repeat a certain execution for a specific amount of times. This allows us to write a single line of code instead of numerous lines of code. The for loop has the following
It is divided into four sections:
- Initialization: When the loop begins, the initial condition is executed once. We can either initialise the variable or use one that has already been initialised. It’s an optional condition.
- Condition: This is the second condition, which is run every time the loop is tested. It keeps running until the condition is false. It must return either true or false as a boolean value. It is an optional condition.
- Increment/Decrement: It increases or decreases the value of the variable.It is an optional condition.
- Statement: The loop’s statement is executed each time the second condition is false.
for(initialization; condition; increment/decrement){ //…statement or code to be executed } |
You can declare and initialize a variable in the initialization stage. The variable can then be evaluated against a condition, and the variable’s value can be incremented or decremented in the last step. The loop will continue to run until the condition is false. The program will then exit the loop and proceed with the remaining commands.
Example 1: Printing Numbers From 1 to 5
#include <iostream> using namespace std; int main() { for (int i = 1; i <= 5; ++i) { cout << i << " "; } return 0; }
Output
1 2 3 4 5
Iteration | Variable | i <= 5 | Action |
---|---|---|---|
1st | i = 1 | true | 1 is printed. i is increased to 2. |
2nd | i = 2 | true | 2 is printed. i is increased to 3. |
3rd | i = 3 | true | 3 is printed. i is increased to 4. |
4th | i = 4 | true | 4 is printed. i is increased to 5. |
5th | i = 5 | true | 5 is printed. i is increased to 6. |
6th | i = 6 | false | The loop is terminated |
Example
#include <stdio.h> int main() { int i=0; for (i = 1; i <= 5; i++) { printf( "Developers Dome\n"); } return 0; }
Output
Developers Dome
Developers Dome
Developers Dome
Developers Dome
Developers Dome
Iteration | Variable | i <= 5 | Action |
---|---|---|---|
1st | i = 1 | true | Developers Dome is printed and i is increased to 2. |
2nd | i = 2 | true | Developers Dome is printed and i is increased to 3. |
3rd | i = 3 | true | Developers Dome is printed and i is increased to 4. |
4th | i = 4 | true | Developers Dome is printed and i is increased to 5. |
5th | i = 5 | true | Developers Dome is printed and i is increased to 6. |
6th | i = 6 | false | The loop is terminated |
Nested For loop:
for(initialization; condition; increment/decrement)
{
for(initialization; condition; increment/decrement)
{
statement;
}
}
Pyramid Example using nested loop
#include <iostream> using namespace std; int main(){ for(int i=1;i<=6;i++){ for(int j=1;j<=i;j++){ cout<<" * "; } cout<<"\n";// for new line }
Output:
* ** *** **** ***** |
Infinite for Loop in C++
#include <iostream> using namespace std; int main(){ int sum = 0; for (int i = 1; i <= 10; --i) { cout<<"Developers Dome"; } }
In this case, the test expression, i<= 10, is never false, and Developers Dome is printed until memory runs out.
You may like:
Type Conversion In C++ Programming
Pingback: While and do...while loop with Example | C++ Programming