For Loop - Execution Flow in C++


C++ uses the brackets ({}) to indicate which lines of code are part of a for loop and which are not.

Take this code for example:

vector<string> languages = {"Python", "Java", "JavaScript"};

for (string language : languages) {
  cout << "I love " + language << endl;
  cout << "I want to get better at it" << endl;
}

Both cout << statements are part of the loop since they are written inside the brackets.

This means that both messages will be printed for every language in the languages list. The output of that code would be:

I love Python
I want to get better at it
I love Java
I want to get better at it
I love JavaScript
I want to get better at it

As for this code:

vector<string> languages = {"Python", "Java", "JavaScript"};

for (string language : languages) {
  cout << "I love " + language << endl;
}
cout << "I want to get better at them" << endl;

The cout << "I want to get better at them" << endl; is not part of the loop.

This means that only cout << "I love " + language << endl; will be executed for every language in the languages list.

Then, when the for loop is over, the cout << "I want to get better at them" << endl; will be executed only once.

The output of this program would be:

I love Python
I love Java
I love JavaScript
I want to get better at them

Execution flow

Lastly, note that the execution of a program always begins on the first line. The code is then executed one line at a time from top to bottom. This is known as execution flow and is the order a program in C++ executes code.

For example, this program:

vector<string> fruits = {"banana", "apple", "kiwi"};

cout << "I'm hungry!" << endl;

for (string fruit : fruits) {
  cout << "I'm gonna eat:" << endl;
  cout << fruit << endl;
}
  

cout << "I love my life!" << endl;

prints:

I'm hungry!
I'm gonna eat:
banana
I'm gonna eat:
apple
I'm gonna eat:
kiwi
I love my life!


Assignment:

Add a for loop inside our code in the editor such that the final program would print:

Today we'll learn about:
Python
Java
JavaScript
Let's code!

Solution

To achieve the desired output, we need to add a for loop that iterates over the list of languages and prints each one. Here is the detailed solution:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main() {
    // Define a vector of strings containing the programming languages
    vector<string> languages = {"Python", "Java", "JavaScript"};

    // Print the initial statement
    cout << "Today we'll learn about:" << endl;

    // For loop to iterate over each language in the vector
    for (string language : languages) {
        // Print each language
        cout << language << endl;
    }

    // Print the final statement
    cout << "Let's code!" << endl;

    return 0;
}

Explanation

Let's break down the solution step-by-step:

  1. Include necessary headers: We include the headers <iostream>, <vector>, and <string> to use the standard input-output stream, vector, and string classes respectively.
  2. Using the standard namespace: We use the using namespace std; directive to avoid prefixing the standard library names with std::.
  3. Define the main function: The main function is the entry point of the program.
  4. Initialize the vector: We initialize a vector of strings named languages with the values "Python", "Java", and "JavaScript".
  5. Print the initial statement: We use cout to print "Today we'll learn about:" followed by a newline.
  6. For loop: We use a range-based for loop to iterate over each element in the languages vector. For each iteration, the current element is stored in the variable language.
  7. Print each language: Inside the loop, we print the current language followed by a newline.
  8. Print the final statement: After the loop, we print "Let's code!" followed by a newline.
  9. Return statement: The main function returns 0 to indicate successful execution.

Output

The output of the program will be:

Today we'll learn about:
Python
Java
JavaScript
Let's code!

Conclusion

In this lesson, we learned about the execution flow of a for loop in C++. We saw how to use brackets to define the scope of the loop and how to iterate over a vector of strings. Understanding the execution flow is crucial for writing correct and efficient programs. By practicing with different examples, you can master the use of for loops and improve your programming skills.

Additional Resources