Section 5h

Combining Mathematics and Assignment


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Packaging The Updates

Making Things Easier.

As you have seen, the assignment operator (i.e., "=") is the way to place data into a variable. In the Java and other programming languages, there are some extended assignment operations that reduce the amount you have to type. They are combined operations, such as addition. As you observed above, you must tell the compiler what data to hold in a variable, so when you add another value, you would normally add it as shown.

subtotal = subtotal + itemPrice;
subtotal = subtotal + tax;

These can be replaced with the following.

subtotal += itemPrice;
subtotal += tax;

The "+=" operator combines the addition of the left variable to the right variable with the result being assigned to the left variable. Both of the above examples accomplish the same thing. The "+=" is a special case of an assignment operator which you can use if you would like, or not.

Here are some others.

// these actions do exactly the same thing
subtotal = subtotal - couponValue;
subtotal -= couponValue;

// these actions do exactly the same thing
multiplyBy5 = multiplyBy5 * 5;
multiplyBy5 *= 5;
divideBy5 = divideBy5 / 5;
divideBy5 /= 5;


Since these shortcuts are local to Java and a few other languages, it will be your option to use either operation. However, many provided examples will commonly use the combined operators, and you are likely to be called upon to recognize these actions in others' code.

As an additional note, when you are using String class objects, you can use the "plus-equal" operation to concatenate, or link strings together, as well.

Consider the following.

// string initializations
string nameString = "William";
string middleStuff = " H. ";
string lastName = "Smithers";

// one form of concatentation
nameString = nameString + middleStuff + lastName;

// another form of concatenation
nameString += middleStuff;
nameString += lastName;

When the above code is implemented, the variable nameString will hold "William H. Smithers". Note that it was important to place the spaces before and after the middle initial, or all of the names would be jammed together.

Easier incrementing. Given all the other tricks you can conduct with most programming languages, there is one that that has become more popular in some languages. There are many situations where you want to increment or increase the value of an item by one, or you might want to decrease the value of the item by one.

This can be done by using one of the tricks you just learned, as shown.

incrementBy1 = incrementBy1 + 1; // one way
incrementBy1 += 1; // better way

However, there is even an easier way to increment data values by one, shown here.

incrementBy1++; // best way

When you have a data value that can be increased by one, just add a "plus-plus" to the end of the variable and your wish will be fulfilled. This is very commonly used for variables that need to be incremented regularly as part of some other process such as looping, it is easy to do, and it is easy to understand by others reading your code.

It is called the increment operator, and it does exactly that. You can probably figure out what the decrement operator does, and how it looks, but an example is given below just for completeness.

decrementBy1 = decrementBy1 - 1; // one way
decrementBy1 -= 1; // better way
decrementBy1--; // best way

It is important to note that all three of the above operations do exactly the same thing, just as the three increment examples above do. The operators simply allow you choices for use in your coding. Another thing to note is that you may place the "++" or the "--" on the left side of the variable as a prefix operation (e.g., ++incrementBy1). It will have a very slightly different, but potentially critical change of meaning if used under certain conditions, but those conditions involve conducting more than one operation in one code line, and may significantly reduce readability. It also has the potential to introduce a difficult to recognize bug into your program, so your best bet is to not pursue this strategy.

Follow two rules when using the increment/decrement operators.

  1. always place the operators at the right side of the variable

  2. always implement the increment or decrement operations all by themselves on one statement line. In other words, do not combine the increment or decrement operations with other operations such as in parameter lists or in array indices (when you get to that point). There are legal ways to do this, but as mentioned previously, it has the potential to significantly contribute to code that is difficult to read, it has a strong potential to introduce bugs into the program, and it is therefore automatically tossed out of good programming operations

This is not a very long topic but it adds another set of actions to your toolbox. It is worth your time to keep trying these things out. Watch the following video to learn more about advanced assignment activities.