Changing Variables in JavaΒΆ

Remember that a variable holds a value and that value can change or vary. If you use a variable to keep score you would probably increment it (add one to the current value). You can do this by setting the variable to the current value of the variable plus one (score = score + 1) as shown below.



        

( lccv1 )

You can set one variable’s value to a copy of the value of another variable. This won’t change the value of the variable that you are copying from.



        

( lccv2 )

Check your understanding

3-3-1: What are the values of x, y, and z after the following code executes?

int x = 0;
int y = 1;
int z = 2;
x = y;
y = y * 2;
z = 3;





Mixed up programs

3-3-2: The following has the correct code to 'swap' the values in x and y (so that x ends up with y's initial value and y ends up with x's initial value), but the code is mixed up and contains one or more extra statements. Drag the needed blocks from the left into the correct order on the right. Check your solution by clicking on the Check Me button. You will be told if any of the blocks are in the wrong order or if you need to remove one or more blocks.