Menu
Coddy logo textTech

Iterating with a For Loop

Part of the Logic & Flow section of Coddy's C++ journey — lesson 14 of 56.

Now that you know how to access individual elements and check the size of a vector, let's learn how to process every element systematically using a traditional for loop. This approach combines the .size() method with index-based access to visit each element in sequence.

A traditional for loop uses an index variable that starts at 0 and increments until it reaches the vector's size:

std::vector<std::string> names = {"Alice", "Bob", "Charlie"};

for (int i = 0; i < names.size(); i++) {
    std::cout << names[i] << std::endl;
}

In this example, the loop condition i < names.size() ensures we don't go beyond the vector's bounds, while names[i] accesses each element using the current index. The loop will print each name on a separate line.

This pattern is fundamental for processing collections of data. The index variable i gives you precise control over which element you're working with, making it useful when you need to know the position of each element or when you want to modify elements during iteration.

challenge icon

Challenge

Easy

Create a program that uses a traditional for loop to iterate through a vector of product names and display them in a numbered list format.

The following inputs will be provided:

  • An integer n representing the number of products
  • Then n strings representing product names

Your program should:

  1. Create a std::vector<std::string> named products
  2. Use push_back() to add each of the n product names to the vector
  3. Use a traditional for loop with an index variable to iterate through the vector
  4. In the loop, use the .size() method for the loop condition
  5. Use the square bracket [] operator to access each element by index
  6. Print each product with its position number (starting from 1) in the specified format

Use the following exact output format:

Product 1: [first product name]
Product 2: [second product name]
Product 3: [third product name]
...

The for loop should start with index 0, use i < products.size() as the condition, and increment i in each iteration. When printing, add 1 to the index to display position numbers starting from 1.

Cheat sheet

Use a traditional for loop to iterate through a vector by combining .size() with index-based access:

std::vector<std::string> names = {"Alice", "Bob", "Charlie"};

for (int i = 0; i < names.size(); i++) {
    std::cout << names[i] << std::endl;
}

The loop condition i < names.size() ensures you don't go beyond the vector's bounds, while names[i] accesses each element using the current index. The index variable gives you precise control over which element you're working with and allows you to know the position of each element.

Try it yourself

#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main() {
    // Read the number of products
    int n;
    cin >> n;
    
    // Create a vector to store product names
    vector<string> products;
    
    // Read product names and add them to the vector
    for (int i = 0; i < n; i++) {
        string product;
        cin >> product;
        products.push_back(product);
    }
    
    // TODO: Write your code below
    // Use a traditional for loop to iterate through the vector
    // and print each product with its position number
    
    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow