Assigning a new value
A variable is a named place in memory that holds a value of one type. You can put a different value there at any time with the assignment operator =. Declaring a variable and giving it a value for the first time is initialization. Every later = is reassignment:
int myVar = 5; // declared and initialized with 5
myVar = 10; // reassigned: no type in front
cout << myVar << endl; // 10
The old value is gone. Wherever myVar appears after the assignment, the program uses 10. The difference between the two lines is the type: int myVar = 5; creates a variable, and myVar = 10; only changes one that exists.
Copying one variable into another
The right side of = can be any expression, including another variable. The value is copied:
int myVar = 5;
int myNum = 10;
myVar = myNum;
myNum = 99;
cout << myVar << endl; // 10
cout << myNum << endl; // 99
After myVar = myNum; both variables hold 10, but they are independent. Changing myNum afterwards does not touch myVar. The same holds for string in C++: assignment copies the text.
The new value often comes from the old one. The right side is calculated first, using the current value, and then stored:
int age = 24;
age = age + 10; // 34
age += 1; // 35, the same as age = age + 1
age++; // 36
Rules and errors to know
- Do not repeat the type.
int age = 24;followed byint age = 34;in the same scope is an error:redeclaration of 'int age'. Writingstring age = "x";there is also an error (conflicting declaration). To change a variable, writeage = 34;. - The type stays the same. A C++ variable cannot change type.
age = "old";is rejected:invalid conversion from 'const char*' to 'int'. - Numbers convert, with loss. Assigning a
doubleto anintcompiles and drops the fraction: afterint x = 5; x = 3.9;the value ofxis3, and-3.9gives-3. constvariables cannot be reassigned. Afterconst int c = 3;, the linec = 4;fails withassignment of read-only variable 'c'.=is not==.x = 5stores 5.x == 5compares. Mixing them up inside anifcondition compiles and silently changesx.- Order matters. The program runs top to bottom, and each statement sees the values left by the ones before it. To exchange two variables you need a third:
int t = a; a = b; b = t;. Writinga = b; b = a;leaves both with the oldb.
The exercise, step by step
The lesson prints a short story twice, before and after a change. It starts with these variables:
role = "Junior Developer"
age = 24
language = "Java"
The code prints them, then ten years pass. You write the lines between the two groups of output:
- Reassign
role. After ten years he is a Senior Developer:role = "Senior Developer";. There is nostringin front, becauserolealready exists. - Reassign
age. Ten years older than 24 is 34:age = 34;. - Leave
languagealone. He still codes in Java, so the last output must still printJava.
string role = "Junior Developer";
int age = 24;
string language = "Java";
// Today
cout << "His age is " << age << endl;
cout << "He is a " << role << endl;
cout << "He codes in " << language << endl;
// ---------------------------------------
// Write your code here:
role = "Senior Developer";
age = 34;
// ---------------------------------------
// Ten years later:
cout << "Ten years later, his age is " << age << endl;
cout << "He is a " << role << endl;
cout << "He still codes in " << language << endl;
The output is:
His age is 24
He is a Junior Developer
He codes in Java
Ten years later, his age is 34
He is a Senior Developer
He still codes in Java
The first three lines show the old values because those statements ran before the reassignment. The lesson uses string, cout and endl without std::. In a file of your own, add using namespace std; or the std:: prefix, plus #include <iostream> and #include <string>.
Key takeaways
- Declare a variable once, with its type. Change it later with
name = value;. - Reassignment replaces the old value, and the right side is evaluated before the store, so
age = age + 10works. - Assigning one variable to another copies the value. The two stay independent.
- A variable keeps its type for life, and
constvariables cannot be reassigned.
