std::cout << 7 / 2 << " " << 7 % 2;
/ is integer division.% operator gives the remainder.% ('mod') operator hands you what's left over after dividing. This int-division gotcha bites everyone at some point, so keep an eye out for it.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.
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.
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.
A quick sweep from basics to intermediate. Don't worry about a score — it just shows what to focus on.
std::cout << 7 / 2 << " " << 7 % 2;
/ is integer division.% operator gives the remainder.% ('mod') operator hands you what's left over after dividing. This int-division gotcha bites everyone at some point, so keep an eye out for it.int x = 5; double y = x / 2; std::cout << y;
x / 2 — what types are x and 2 at that moment?x / 2.0. Order matters — the math runs with the types it starts with.for (int i = 0; i < 3; i++)
std::cout << i;int x = 5;
if (x = 3)
std::cout << "yes";= means assign; two == means compare.x = 3 assigns 3 to x (it doesn't compare!), and that result (3) counts as true, so the if runs and prints 'yes'. He almost certainly meant if (x == 3).= stuffs a value into a variable; double == asks 'are these equal?' Here the single = quietly changes x to 3, then the if sees a nonzero number, which counts as true. The fix is ==. Any time an if 'always runs,' check your equals signs first.int a[3] = {10, 20, 30};
std::cout << a[1];std::string s = "cat"; std::cout << s.length();
.length() counts them..length() (same as .size()) tells you how many. The dot means 'ask this string to do something' — strings come with lots of built-in abilities like this. Super handy for loops that walk through each character.int square(int n) {
return n * n;
}
// ... later ...
std::cout << square(4);return hands back an answer. The int in front says 'this machine gives back an int.' Calling square(4) plugs in 4 and gets 16 out — and you can reuse it as many times as you want with different numbers.int i = 1, sum = 0;
while (i <= 4) {
sum += i;
i++;
}
std::cout << sum;sum += i means 'add i onto sum.'sum += i is shorthand for sum = sum + i. And i++ is what eventually makes the test false; forget it and you get an infinite loop.std::cout << (3 > 2 && 5 < 1);
&& means AND — both sides must be true.&& (AND) is strict — every part has to be true, or the whole thing collapses to false. C++ shows booleans as numbers: 1 for true, 0 for false. Its cousin || (OR) is the opposite — it only needs one side true. Those two operators are most of what you need for if-statements.void change(int x) { x = 10; }
// ... later ...
int a = 5;
change(a);
std::cout << a;int& x (a reference).change works on a copy of a, so setting x = 10 inside doesn't touch the original a. (If it were int& x, a would become 10.)&, which hands over the original itself. This copy-vs-reference idea is a big deal in C++.The building blocks: storing values, printing them, reading input, and the classic type traps.
int age = 13; std::cout << "Age: " << age;
cout is how you print. The << (little arrows aimed at cout) feed it things to show, left to right. Text in quotes prints literally; a variable name prints its value. You can chain as many << as you want in one line.% gives the leftover, and the moment one number has a decimal point the division goes 'normal' and keeps the fraction. If a calculation is supposed to have decimals, make sure at least one value is a double.char c = 'A'; std::cout << (int)c;
(int) reveals it.(int)c converts the character to its number code, and 'A' is 65.char is just a small number that usually shows up as a letter. Normally cout shows the letter, but (int) says 'show me the number instead.' That's also why 'A' + 1 gives 'B' — you're quietly doing math on those hidden codes.int n; std::cin >> n; std::cout << n * 2;
cin >> n stores the typed value into n; then it's used like any variable.cin >> n reads 6 into n, then it prints n × 2 = 12.cin is the input twin of cout, and the arrows point the other way (>>) — into the variable, because data is flowing in. A nice way to remember it: the arrows always point where the data is going. Out to the screen with <<, in from the keyboard with >>.std::cout << "Sum: " << 2 + 3;
<< ?+ happens before <<, so 2 + 3 becomes 5 first, then it prints.+ binds tighter than the << printing operator, so the addition finishes first. When in doubt, wrap the math in parentheses — << (2 + 3) — so you never have to memorize the whole precedence table.Making decisions and repeating work — the heart of every program.
int score = 72; if (score >= 90) std::cout << "A"; else if (score >= 70) std::cout << "B"; else std::cout << "C";
int total = 0;
for (int i = 1; i <= 5; i++)
total += i;
std::cout << total;for (int i = 2; i <= 10; i += 2)
std::cout << i << " ";i += 2, so i goes up by 2 each time.i++ — here i += 2 counts by twos. Change the start, the test, and the step and you can make a loop march however you want: by 2s, backwards, by 10s. Those three slots are your control panel.int n = 3;
while (n > 0) {
std::cout << n << " ";
n--;
}n-- lowers n by 1 each pass; the loop ends when n reaches 0.n > 0 fails.n-- counts down. Leave that out and n stays 3 forever: an infinite loop. Always make sure every loop has a way to end.for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
std::cout << i;
}continue skips just that one pass (not the whole loop).continue skips the rest of that one pass, so when i is 3 it prints nothing and jumps to i = 4.continue means 'skip the rest of this loop pass and go to the next one' — so 3 gets skipped but the loop keeps going. Its stronger cousin break means 'stop the whole loop right now.' Two handy escape hatches for when a loop needs to react mid-stream.for (int i = 1; i <= 5; i++) {
if (i % 2 == 0) std::cout << "E";
else std::cout << "O";
}i % 2 == 0 is true when i is even.i % 2 == 0 asks 'is i even?': 1 is odd (O), 2 even (E), 3 O, 4 E, 5 O.i % 2 is the go-to even/odd test: an even number divided by 2 has remainder 0, an odd one has remainder 1. So i % 2 == 0 literally asks 'is it even?' This remainder trick shows up everywhere — every-3rd, every-5th, and the classic FizzBuzz all use it.Reusable machines: hand them inputs, get an answer back, and keep your code clean instead of copy-pasting.
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.
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.
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.int add(int a, int b) {
return a + b;
}
// ... later ...
std::cout << add(3, 4);add(3, 4) runs the function with a = 3 and b = 4, and returns 3 + 4 = 7.int a, int b — are its inputs (called parameters), and return hands an answer back out. The int in front promises the answer is an int. Calling add(3, 4) feeds in 3 and 4, and 7 comes back to print.void greet(std::string name) {
std::cout << "Hi " << name << "!";
}
// ... later ...
greet("Owen");void means the function does a job (printing) but doesn't hand a value back.greet takes the text "Owen" as name and prints the greeting. It returns nothing.void means 'gives nothing back.' Some functions hand you a value (like add), and some just do something — here, print. A void function has no return value; you call it for its action, not for an answer.int dbl(int n) {
return n * 2;
}
// ... later ...
std::cout << dbl(5) << " " << dbl(10);dbl doubles whatever you give it: dbl(5) = 10, dbl(10) = 20.* 2 all over. Bonus: if you ever change how doubling works, you change it in exactly one place.void reset(int x) {
x = 0;
}
// ... later ...
int score = 50;
reset(score);
std::cout << score;reset got a copy of score, not score itself.reset changes its own copy of the value, so the real score is untouched.reset scribbles 0 on the copy, but your original score stays 50. This is 'pass by value,' and it's the default. (You met this in the diagnostic — it's worth seeing twice.)&.void addTen(int& x) {
x += 10;
}
// ... later ...
int n = 5;
addTen(n);
std::cout << n;& makes x a reference — it is n, not a copy of it.int& x means x is really n itself, so x += 10 changes n to 15.& on the parameter and you pass a reference instead of a copy — you hand over the real variable, not a photocopy. So changes inside the function stick. Copy (safe, separate) versus reference (shared, changes stick) is one of the biggest ideas in C++, and that single & is the whole difference.int makeValue() {
int result = 42;
return result;
}
int main() {
makeValue();
std::cout << result; // this line
}result actually exist — everywhere, or only inside makeValue?result is local to makeValue, so main can't see it. To use the value, catch what the function returns: int r = makeValue(); std::cout << r;returning it, and the caller catches it in its own variable.bool isEven(int n) {
return n % 2 == 0;
}
// ... later ...
if (isEven(6)) std::cout << "yes";
else std::cout << "no";isEven(6) hands back true or false — recall that 6 % 2 is 0.n % 2 == 0 is true, isEven(6) returns true, and the if runs.bool (true/false), which makes your ifs read like plain English: if (isEven(6)). Under the hood it's the same % even-test from the last lesson, just wrapped in a named machine you can reuse. Naming a condition this way is a clean habit — if (isEven(x)) beats scattering x % 2 == 0 everywhere.