Codeloft
0 0 XP

Variables and loops

8 min 20 XP + 10 for no hints

C++ needs to know a variable's type. int holds whole numbers: int total = 0;

A for loop repeats code. for (int i = 1; i <= 5; i++) starts i at 1, keeps going while i <= 5, and adds 1 each time.

total += i; is short for total = total + i;. Open Step through to watch i and total change on every pass.

Example
for (int i = 1; i <= 3; i++) {
    std::cout << "Round " << i << std::endl;
}
Output
Round 1
Round 2
Round 3
Changing the number in the loop condition changes how many times the loop runs.

Your tasks

0 of 3 done
  1. Change the loop so it adds the numbers 1 to 10
  2. Store your favorite number in a new int variable
  3. Print that variable with std::cout

Quick check

In the starter code, what is total after the loop?

loop.cppChecked as you type
No problems found. Unclosed tags, missing semicolons and errors show up here in plain words.