Start learning free

LESSON 12 OF 409 · VARIABLES

Variable Reassignment in C++: Changing a Variable's Value

Lesson Read the full explanation

You can always change the value stored in a variable with the assignment operator(=):

// Initializing with 5:
int myVar = 5;

// Assigning the value 10:
myVar = 10;

cout << myVar; // Output: 10

First, this code creates a variable named myVar and initializes it with 5.

Then, the code assigns 10 to myVar. Now, when myVar appears again in the code, the program will treat it as if it is 10.


Assigning the Value of One Variable to Another:

You can also assign the value of some variable to another variable using the assignment operator:

int myVar = 5;
int myNum = 10;

myVar = myNum;

cout << myVar; // Output: 10

First, this code creates a variable named myVar and initializes it with 5 and another variable named myNum and initializes it with 10.

Then, the contents of myNum (which is 10) is assigned to the variable myVar. Now, myVar also has the value of 10.


Assignment
Follow the Coding Tutorial and let's reassign some variables.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    C++ · VARIABLES

    Variable Reassignment in C++, explained

    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 by int age = 34; in the same scope is an error: redeclaration of 'int age'. Writing string age = "x"; there is also an error (conflicting declaration). To change a variable, write age = 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 double to an int compiles and drops the fraction: after int x = 5; x = 3.9; the value of x is 3, and -3.9 gives -3.
    • const variables cannot be reassigned. After const int c = 3;, the line c = 4; fails with assignment of read-only variable 'c'.
    • = is not ==. x = 5 stores 5. x == 5 compares. Mixing them up inside an if condition compiles and silently changes x.
    • 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;. Writing a = b; b = a; leaves both with the old b.

    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:

    1. Reassign role. After ten years he is a Senior Developer: role = "Senior Developer";. There is no string in front, because role already exists.
    2. Reassign age. Ten years older than 24 is 34: age = 34;.
    3. Leave language alone. He still codes in Java, so the last output must still print Java.
    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 + 10 works.
    • Assigning one variable to another copies the value. The two stay independent.
    • A variable keeps its type for life, and const variables cannot be reassigned.
    Keep going with C++About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free