Free Lesson · Intro to C++

C++, from the ground up.

Restarting after a break? Start with the quick diagnostic to see where you are, then work the lessons. Every exercise is try-it-first: take a hint if you're stuck, reveal the answer, then open the deeper explanation to really get the why.

Placement diagnosticLessons & exercisesTry → hint → reveal → deeper
How to use this: read each snippet and predict the output before you reveal anything. Stuck? Tap Show a hint (they build up one at a time). Then Reveal answer, and open Deeper answer for the plain-English reason it works. Mark each one Got it or Review as you go.
Why learn this?

You get to build the stuff you use.

Games, apps, YouTube, the phone in your pocket — all of it is code. C++ is the fast, powerful language behind a huge amount of it, especially games and anything that needs to be quick. Learning it flips you from just using technology to building it — and it trains you to think in clear, logical steps, which quietly helps in every other subject too.

The one big idea

A program is a recipe.

A computer is lightning-fast but painfully literal — it does exactly what you write, in order, with zero guessing. So coding is just writing a recipe so precise a machine can follow it. Variables are labeled jars you store things in, loops say 'repeat this step,' and if-statements say 'if this, then that.' Most bugs are just the recipe saying something a little different from what you meant — that's the whole = vs == trap. Once you picture code as a recipe, the rest clicks into place.

Placement Diagnostic (start here)

A quick sweep from basics to intermediate. Don't worry about a score — it just shows what to focus on.

1
What does this print?
std::cout << 7 / 2 << " " << 7 % 2;
A) 3.5 3.5   B) 3 1   C) 3.5 1   D) 4 1
2
What prints?
int x = 5;
double y = x / 2;
std::cout << y;
A) 2.5   B) 2   C) 1.5   D) error
3
Output?
for (int i = 0; i < 3; i++)
    std::cout << i;
4
What happens here?
int x = 5;
if (x = 3)
    std::cout << "yes";
A) prints nothing   B) prints "yes", and x becomes 3   C) compiler error   D) prints "yes", x stays 5
5
Output?
int a[3] = {10, 20, 30};
std::cout << a[1];
6
Output?
std::string s = "cat";
std::cout << s.length();
7
Output?
int square(int n) {
    return n * n;
}
// ... later ...
std::cout << square(4);
8
Output?
int i = 1, sum = 0;
while (i <= 4) {
    sum += i;
    i++;
}
std::cout << sum;
9
Output?
std::cout << (3 > 2 && 5 < 1);
A) true   B) 1   C) 0   D) error
10
Output?
void change(int x) { x = 10; }
// ... later ...
int a = 5;
change(a);
std::cout << a;
A) 10   B) 5   C) error

Lesson 1 · Variables, types & input/output

The building blocks: storing values, printing them, reading input, and the classic type traps.

1
What does this print?
int age = 13;
std::cout << "Age: " << age;
2
Compute each (C++ int/double rules): (a) 9 / 4   (b) 9 % 4   (c) 9.0 / 4
3
What prints? (ASCII code for 'A' is 65)
char c = 'A';
std::cout << (int)c;
4
If the user types 6, what prints?
int n;
std::cin >> n;
std::cout << n * 2;
5
This should print Sum: 5. What actually prints, and why?
std::cout << "Sum: " << 2 + 3;

Lesson 2 · Control flow (if, for, while)

Making decisions and repeating work — the heart of every program.

1
Output?
int score = 72;
if (score >= 90) std::cout << "A";
else if (score >= 70) std::cout << "B";
else std::cout << "C";
2
What prints?
int total = 0;
for (int i = 1; i <= 5; i++)
    total += i;
std::cout << total;
3
What prints?
for (int i = 2; i <= 10; i += 2)
    std::cout << i << " ";
4
Output?
int n = 3;
while (n > 0) {
    std::cout << n << " ";
    n--;
}
5
What prints?
for (int i = 1; i <= 5; i++) {
    if (i == 3) continue;
    std::cout << i;
}
6
What's the full output?
for (int i = 1; i <= 5; i++) {
    if (i % 2 == 0) std::cout << "E";
    else std::cout << "O";
}

Lesson 3 · Functions

Reusable machines: hand them inputs, get an answer back, and keep your code clean instead of copy-pasting.

Why functions?

Write it once. Use it forever.

Right now, when you need the same few lines again, you copy-paste them — and if there's a bug, you get to fix it in ten places. A function lets you write that logic once, give it a name, and call it whenever. Real programs aren't one giant wall of code; they're lots of small, named machines calling each other. This is the jump from writing a script to building a program.

The mental model

A function is a machine.

Picture a vending machine. You put something in the slot (the inputs, called arguments), it does its hidden work, and a result drops out the bottom (return). When you use it you don't care how it works inside — you just press the button. Some machines hand a value back, like add(3,4) giving 7; a void machine just does a job, like a light switch — flip it, nothing drops out. See functions as little machines you build and reuse, and the rest is just wiring them together.

Watch for one idea all lesson: the value a function hands back with return disappears unless you catch it — print it, or store it in a variable. And the difference between a copy and a reference (that little &) decides whether a function can change your original variable. Those two ideas are most of what's tricky about functions.
1
What prints?
int add(int a, int b) {
    return a + b;
}
// ... later ...
std::cout << add(3, 4);
2
What prints?
void greet(std::string name) {
    std::cout << "Hi " << name << "!";
}
// ... later ...
greet("Owen");
3
What prints?
int dbl(int n) {
    return n * 2;
}
// ... later ...
std::cout << dbl(5) << " " << dbl(10);
4
Output?
void reset(int x) {
    x = 0;
}
// ... later ...
int score = 50;
reset(score);
std::cout << score;
A) 0   B) 50   C) error
5
Now what prints? Notice the &.
void addTen(int& x) {
    x += 10;
}
// ... later ...
int n = 5;
addTen(n);
std::cout << n;
6
Will this compile?
int makeValue() {
    int result = 42;
    return result;
}
int main() {
    makeValue();
    std::cout << result;   // this line
}
A) prints 42   B) compiler error
7
What prints?
bool isEven(int n) {
    return n % 2 == 0;
}
// ... later ...
if (isEven(6)) std::cout << "yes";
else std::cout << "no";