Home » C++ Tutorial » For Loop in C++ for Beginners

For Loop in C++ for Beginners

Often times while programming we may have instances where we want our code to perform certain operations multiple times. Instead of retyping the same code, again and again, we can make use of handy loops in C++, viz., while loops and for loops in C++. In this article, let’s take a run down one specific type of loop, i.e., the ‘for loop’ in C++. For example, if you were to print out “Roses are red.” 5 times, would you retype the code 5 times or perhaps, put that code into a function and apply the function 5 times? This is where a loop comes to be useful. Using a loop, the same thing can be done in the following method in C and C++.

C

/ C program to illustrate for loop 
  
#include <stdio.h> 
  
int main() 
{ 
    int i = 0; 
  
    // Writing a for loop 
    // to print Roses are red. 5 times 
    for (i = 1; i <= 5; i++) { 
        printf("Roses are red.\n"); 
    } 
  
    return 0; 
}

C++

// C++ program to demonstrate for loop 
  
#include <iostream> 
using namespace std; 
  
int main() 
{ 
  
    // Writing a for loop 
    // to print Roses are red. 5 times 
    for (int i = 1; i <= 5; i++) { 
        cout << "Roses are red.\n"; 
    } 
  
    return 0; 
}

Output:

Roses are red.

Roses are red.

Roses are red.

Roses are red.

Roses are red.

How to use For Loops in C++

Using for loops in C++ is quite simple if we break the process down to understand it better.

For loops typically start with the keyword ‘for’ and break up into 3 different parts. Each of these 3 parts is separated by a semi colon (;) The 3 different parts used in for loops are as follows:

#1. Variable declaration:

This part is where you need to define the data that you want to work with. For instance, 

for (int i = 1)

In this case, you can use another data type instead of the integer, your variable can be any character but the letter ‘i’ is used as a standard and the value of this variable can be as per the coders preferences instead of sticking to 0.

#2. Condition:

This is the part of the process where the coder defines what parameters the function should reach so that the loop may repeat. As long as the defined parameters are met, the loop will keep repeating for the set number of times. Using our example above, the second part for our code will be as follows:

for (int i = 1; i<=5)

#3 Updation:

This is the stage within the for loop where you define how you would like your variable to progress. In the case of our aforementioned example, it would go something like this:

for (int i = 1; i<=5; i++)

The ‘i++’ in this case just implies that the variable will have one increment after every iteration. Of course, this could also be replaced by ‘i += 1’ or ‘i = i + 1’ and still derive the same results.

Reminder: do not forget to separate the 3 parts using a semi colon (;)

You can follow these steps by adding in your body, i.e., the piece of content that you want to loop using the for loop in C++ These loops are widely popular for use in arrays as they make navigating through them easier. Also, gaming developers need to use loops within their code frequently so as to ensure the smooth functioning of their software. Loops in general, within C++, can be nested, i.e., grouped together to decrease clutter, increase processing speed, and maximize efficiency for the coder or developer.

For loop flow in C++

for loop in c++

C++ Standard and for loops:

According to the C++ standard, a variable declared in a for loop is supposed to go out of scope after the end of the for loop. For instance, just like in our above-mentioned example,

for (int i = 1 ; i <= 5 ; i++) {
   // random function
}
// i is no longer in scope under /Za or /Zc:forScope

As it is, under /Ze, the variable that you declare in a for loop will be in scope until the end of that particular for loop. /Zc:forScope allows standard behaviour of variables that you declare in for loops without needing to particularly mention /Za.

The scoping differences of the for loop can also be utilised to redeclare the variables under /Ze like so:

// for_loop9.cpp
int main(){
   int i = 0;   // hidden by var with same name declared in for loop
   for ( int i = 1 ; i <= 5; i++ ) {}

   for ( int i = 1 ; i <= 5; i++ ) {}
}

This function of the code imitates the standard behaviour of variable you declare in a for loop, which needs the variables declared in a for loop to no longer be in scope after the loop ends. Once you declare a variable in a for loop, the compiler promotes it internally to a local variable within the for loop’s enclosing scope. It will be promoted even though there may be a local variable with the same/ similar name.

As you may have witnessed from all of the above, coding may seem like a haunting beast but it can be made simpler and much, much, much easier by using useful tools such as loops, for loops in C++ being the most useful one. You are essentially taking a bigger portion of functions, a bigger workload and by the sheer quality of repetition, making it faster, more convenient and fruitful. Remember, the loop will repeat for as many times as you order it to, the value for that variable and its conditions can be easily changed to arrive at different results to suit your coding needs. Hope this helped you learn a bit more about coding, stay tuned!

Did you know?
1. For Loop In C++
2. Vectors In C++
3. Difference Between C and C++
4. C++ Swap
5. C++ List
6. C++ Hello World
7. C++ Map
8. Converting String To Int C++
9. Stack C++
10. Queue C++

Pin It on Pinterest