what happens when you assign reassign or access a variable in a program let's trace the execution of a program with variables to find out when the program starts running the computer loads the first instruction into its working memory this instruction is an assignment statement the left hand side is the variable name the equal sign is the assignment operator and the right hand side is the value to be stored in that variable the computer looks for any Expressions to simplify on the right hand side but in this case the string is already a single value when
the computer executes an assignment statement it goes off to its short-term memory and looks for a label with that variable name here it looks for the name Gene but it doesn't find it that means it doesn't already know a gene it needs to create one so the computer allocates a new chunk of memory for this new variable it tags that chunk of memory with the name Gene so it can distinguish it from its other chunks of memory then it takes the value on the right hand side of the assignment operator and copies it over to
that location in short-term memory that way it doesn't forget it when it clears out its working memory in a second okay well this instruction is complete so the computer is going to clear out its working memory forget all of this stuff here and move on to the next line however anything that it's stored in its short-term memory stays behind here we have another assignment statement with this one though on the right hand side of the assignment operator there's an unsimplified expression like humans it's much easier for the computer to remember a single value like 11
than the long expression 4 + 5 + 2 so the computer makes sure to simplify any Expressions on the right hand side of the assignment operator down to a single value before it memorizes it now we have 11 so the computer goes off to its short-term memory looks for the label num mutations doesn't find it so creates a new variable allocates a new chunk of memory tags it and copies the value 11 over from working memory once that value is stored this instruction is complete so the computer clears out working memory and moves to the
next line now we have a print instruction the computer looks inside of the parenthesis first for any Expressions to simplify and finds the variable named Gene now this is not an assignment the name Gene is not on the left hand side of an equal sign or assignment operator that means we are accessing the value stored in gene not setting it the computer goes off to its short-term memory looks for the label Gene finds it and then follows it to the location and memory that it points to then it substitutes the value stored there into the
instruction in working memory for the second part note that there are quotation marks here that means that this is just the string Gene not the variable Gene so we're not going off to access anything in short-term memory we're just concatenating these two strings together once we're down to a single value the computer Peaks outside the parentheses asks what it's supposed to do with that value sees print and prints brca2 Gene to the console next line we have a self- referencing assignment statement the variable we're assigning to on the left hand side is no mutations but
we're also accessing the variable numb mutations on the right hand side not a problem because we always evaluate the right hand side first so we access the variable num mutations look up what value it currently has stored there and substitute that in on the right hand side only then we evaluate this expression now that we have a single value we're ready to store the computer sees that it already has a variable numb mutation so it's not going to create a new one instead it'll allocate a new chunk of memory store the value 12 there and
then take that existing no mutations and move it to point to that new chunk of memory that old chunk of memory with that old value is now unreachable so the computer will come in and clear that out at some point to make more room then it clears out and picks up the next line how do you think this assignment works though it may look like it this does not create some special permanent link between the variable Gene and the variable num mutations where they'll forever be the same value now what it does is go off
to its short-term memory looks for the variable Gene and then moves it to point to the same location as numb mutations the computer does this as an optimization because it could have allocated a new chunk of memory stored the value 12 there too and then have Gene point to it in some cases though we may be storing really big values it could take up a lot of space of memory if the computer had to copy over a big value multiple times so it would rather not if it can avoid it and now that this old
location is unreachable it goes away we clear out and we load in the next line on the right hand side of the assignment operator we have an expression so we need to simplify that first we're accessing the variable Gene because it's on the right hand side so we go off to short-term memory grab that value substitute it in and then we just add these integers together we find the label num mutations in short-term memory so we allocate a new chunk of memory store the value 17 there and then move num mutations to point to that
new location note that this doesn't affect the value of Gene we move mutations to point to a different location but we didn't move Gene it still points to that old location with the value 12 because that location is still reachable through the variable Gene it sticks around this last line is just a print function so we evaluate the expression inside the parentheses first we go off we grab the value stored in the mutations we substitute it in and then we do the same for the variable Gene we add those two integers together to get a
single value and then we peek outside the parentheses and see that we need to print that value to the console the computer looks for the next line and finds that it's at the end of the program when the program terminates short-term memory gets cleared so all these variables go away and now the computer has room again to remember new things the next time you run a program