Fundamentals · Getting started
C++ first steps
Start with names, stored numbers and reading one instruction at a time.
Code gives a computer instructions.
A program is instructions a computer can carry out. These small C++ examples describe changing whole numbers. You do not need to install anything: read, predict, then check. We show a few instructions from inside main, the function where a C++ program starts. Each ; ends one of these instructions. Start with what a variable means; the check is optional if you already know this.
A variable has a name and a current value.
A variable is a named place to keep a value. In int score = 3;, int says the value is a whole number, score is the name, and 3 is its starting value. Creating it with that starting value is initialization. The later instruction score = 8; changes the existing variable: that is assignment. Here = means store the value on the right in the name on the left, not ask whether two things are equal.
int score = 3;
score = 8;Keep only the current value.
For these straight-line examples, follow the instructions from top to bottom. First score starts at 3. Next score + 2 reads its current 3 and calculates 5; assignment stores that 5 in score. Finally score = 1 replaces 5 with 1. A useful trace is a note after each line: score is 3, then 5, then 1. Later lines do not change what earlier lines did.
int score = 3;
score = score + 2;
score = 1;A copied number is independent.
int saved = score; creates a second variable with the number score holds now. It does not create a live connection between the names. When score later changes from 4 to 7, saved keeps 4. Think of two separate boxes, not two labels on one box. This picture explains values; it is not a diagram of physical memory addresses.
int score = 4;
int saved = score;
score = 7;- score
- current value: 7
- saved
- current value: 4
A copy is a snapshot, not a subscription.
Imagine writing today's apples count onto a second piece of paper. If the original count changes later, that second paper does not update itself. In this code, ticket remembers 6; count becomes 8. Try the next example with different names: it asks you to track both a copy and an update.
int count = 6;
int ticket = count;
count = 8;Read an update in its longer form.
For the whole-number examples here, score += 2 means score = score + 2: add 2 to the current number and store the result. ++score means add 1 and store it. We use ++ only as an instruction by itself, not inside a larger expression. Text after // is a comment for people; the computer does not execute that text. Here score starts at 3, becomes 5, then becomes 6.
int score = 3;
score += 2; // add two
++score; // add oneA useful place to stop.
You have practiced reading instructions in order, changing a stored number and keeping a copy separate. You can stop here for this visit. If any part is unclear, revisit its example; one right answer does not mean everything is learned. On another visit, try Functions and copies to see how numbers move into and out of a named block of instructions.
Further reading
Trusted resources for the rules and APIs discussed in this guide.