Types and Control Structures

We'll continue our tour of C++ in this lecture, with a particular focus on three areas:

  • Data types, both fundamental and those from the C++ standard library.
  • Control flow structures for branching and looping.
  • Defining and calling functions.
Updated Fall 2025

1: Functions

In more complex programs, it's essential to define functions to abstract away details.




2: Standard Library Types

The C++ Standard Library provides a variety of container and utility types. We'll take a look at a few now, including std::vector which is used extensively in project 1.




3: Iteration
3.1 Not Started

In imperative programming, loops allow us to iterate through a set of instructions multiple times as long as some condition is true. C++ has two primary looping constructs, for and while.


3.1 Exercise: Vector Sum

Fill in the blanks so that the code computes the sum of elements in the vector.

If your code compiles, but you're not getting credit, try clicking the "Simulate" button to step through the code and see where it's going wrong.


Sample solution…

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

  int main() {
    vector<double> v = {1, 5, 3.5, 6.5};

    // Declare accumulator variable to hold the sum
    double sum = 0;

    // Traverse by index from 0 ... v.size()-1
    for (int i = 0; i < v.size(); ++i) {

      // Access each element by index and add to sum
      sum += v[i];
    }

    cout << "Sum: " << sum << endl;
  }

Make sure to return to finish the video after completing the exercise!


4: Branching
4.1 Not Started

The if and else constructs are used for branching in C++, often in conjunction with loops.


4.1 Exercise: Vector Minimum

Fill in the blanks so that the code finds the minimum value in the vector.

If your code compiles, but you're not getting credit, try clicking the "Simulate" button to step through the code and see where it's going wrong.


Sample solution…

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

  int main() {
    vector<double> v = {1, 5, 3.5, 6.5};

    // Keep track of the "best" candidate we've seen.

    double min = v[0];

    for (size_t i = 0; i < v.size(); ++i) {
      // If v[i] is less than the current min, update min.
      if (v[i] < min) {
        min = v[i];
      }
    }

    cout << "Min: " << min << endl;
  }

Make sure to return to finish the video after completing the exercise!


5: Logical Operations and Short-Circuit Evaluation

Sometimes we need to create compound boolean expressions using the &&, ||, and ! operators. In C++ (and some other languages), && and || have special behavior called short-circuit evaluation. Here's the details.




6: break; and continue;

Finally, a miscellaneous topic. C++ also has special break; and continue statements that affect the execution of loops.


You've reached the end of this lecture! Your work on any exercises will be saved if you re-open this page in the same web browser.

Participation Credit
Make sure to sign in to the page, complete each of the exercises, and double check the participation indicator at the top left of this page to ensure you've earned credit.