Loops are an essential feature of programming, and allow coders to automate and repeat the same task multiple times. This allows coders to maintain the readability of their code and also reduces the need to repeat the same code throughout a program.
The for loop is one of the three main loops in C++ used to execute repetitive tasks. for loops execute an action for a predefined number of times.
This tutorial will explore, with references and examples, the basics of loops, how to use for loops in C++, and how to use a for loop to iterate through an array in C++. After reading this guide, you’ll be equipped with all the tools you need to work with C++ for loops.
C++ Loop Refresher
There are three main types of loops in C++ which coders use to perform repetitive tasks. These are:
- for Loops
- while Loops
- do…while Loops
The for loop is used to execute a repetitive task a certain number of times based on a loop counter. While loops are used to execute a block of code while a certain condition evaluates to true. If you’re interested in learning more about while loops in C++, you can read our tutorial on while loops here.
C++ for Loop
The for loop in C++ executes a specific number of times. For example, a for loop could count up to five and print out each number between one and five, or a for loop could iterate through every item in an array and print it to the console.
Here’s the syntax for a for loop in C++:
for (initialization; expression; update) { // Code }
Let’s break this syntax down. There are three components to a for loop, which are as follows:
- initialization is a statement that is executed once when the loop starts.
- expression is the statement that will be evaluated to determine whether the loop should be executed.
- update is executed after the loop has been executed.
The only statement that is required in a for loop is the expression, which is used to determine when the loop should be executed.
Let’s write a loop to illustrate how the for loop works in C++. Suppose we want to print out every number between 0 and 5 We could do so using a for loop. Here’s the code we would use to accomplish this task:
#include <iostream> int main() { cout << "Numbers between 0 and 5: \n"; for (int i = 0; i < 5; ++i) { cout << i << "\n"; } }
Our code returns:
Numbers between 0 and 5: 0 1 2 3 4
Let’s break down our code. First, we use the cout statement to print out Numbers between 0 and 5:
to the console, followed by a new line (denoted by \n
).
We then declare a for loop which should run five times. Here are the components of our for loop:
- int i = 0 is our initialization statement. This declares a variable called i which keeps track of how many times our loop has executed.
- The i < 5 condition is checked for each iteration in the loop. This expression determines whether i is less than 5, and if the condition is true, the loop will execute; otherwise, the loop will terminate.
- i++ is executed after the loop and adds 1 to the variable i. This allows us to keep track of how many times our loop has run.
In the body of the loop statement, we have a line of code that prints out the number stored in the variable i, followed by a new line character.
For loops are commonly used when you need to repeat a block of code for a specific number of times.
C++ Range-Based for Loops
When you’re working with a for loop in C++, you may decide that you want to execute the for loop for a number of times equal to the length of an array.
That’s where the range-based for loop comes in. The range-based for loop is used to iterate over the items in a C++ array. Here’s the syntax for a range-based for loop in C++:
for (dataType item : list) { // Code }
Here are the main components of a range-based for loop in C++:
- dataType is the type of data the loop will process. This should be the same type of data used to store values in list.
- item is used to keep track of an individual item in list.
- list is the list over which you want to iterate in your code.
For instance, suppose you are operating a local soup store and you want to print out your menu for customers to see. You have an array which stores all the soups you are serving, and you want to print out each item in the array individually to the console.
You could use the following code to print out each item on a soup menu to the console:
#include <iostream> #include <string> int main() { string soups[5] = {"Beef Noodle", "Chicken", "Mushroom", "Tomato", "Chicken Noodle"}; cout << "Soups on the menu: \n"; for (string soup : soups) { cout << soup << "\n"; } }
Our code returns:
Soups on the menu: Beef Noodle Chicken Mushroom Tomato Chicken Noodle
As you can see, our code has printed out a list of every item in the soups array to the console. Let’s break down how our code works step-by-step.
First, we import the string library which is used to access the string data type. Then we declare an array called soups which stores five string values.
Next, we print out the message Soups on the menu:
to the console, followed by a new line character (represented by \n
). We then initialize a for loop which iterates over every value in the soups array.
When the for loop executes, each individual item in the soups array is printed to the console, followed by a newline character.
Conclusion
The for loop is used to execute repetitive tasks in C++. for loops execute for a predetermined number of times. For instance, a for loop may be instructed to execute five times, or ten times. for loops that are range-based, execute a number of times equal to the length of an array in C++.
In this guide, we discussed the basics of C++ loops, and how you can use for loops and range-based for loops in your C++ code. Now you have the knowledge you need to start working with for loops in C++ like a pro!
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.