You studied about some operators in java in the previous lesson. Some more are here.
Compound assignment operators:
Let’s suppose a statement
a=a+3;
In mathematics, it does not make sense, but in programming, it works. It means that you are increasing the value of a by 3. Similarly consider the following statement:
a=a-53;
It also means that you are decreasing the value of a by 53.
Similarly the following also work in programming:
a=a*5;
a=a/4;
Such statements are used by programmers many times, so there is a short way to perform this action. There are operators in Java called compound assignment operators.
+=
If we write a+=3 this is equivalent to a=a+3. This will increase the value of a by 3.
-=
If we write a-=2 this is equivalent to a=a-2. This will decrease the value of a by 2.
*=
a*=2 is equivalent to a=a*2 It means that we are multiplying the value of a by 3 and assigning the answer to a.
/=
Same as above. a/=5 means that we are dividing the value stored in the variable a by 5 and then storing the answer back in a.
Let’s take an example:
Let’s take an example that is a bit more practical. The formula for converting celsius temperature to Fahrenheit is F = 1.8 C + 32 . Let’s implement this using compound assignment operators. Note that if we have celsius temperature, then to convert it to Fahrenheit, firstly we will have to multiply it by 1.8 and then add 32 in it. See the program below to convert 37° Celsius to Fahrenheit.
I hope you understood the concept of compound assignment operators very well (and the use of double also) . Now let’s move to the increment and decrement operators.
Increment and decrement operators:
++ and — are the icrement and decrement operators. They are used to increase or decrease the value of a variable by one. An example would explain their use better.
The important concept to understand here is the concept of post increment/decrement and pre increment/decrement operators. We can use this ++ and — before and after the name of the variable on which the operation is supposed to be performed. For example, consider the following example:
Using them before and after the variable name is not always the same. This is better illustrated by the following example.
When the operator is used before the variable name, it is called pre increment or pre decrement, and when it is used after the variable name, it is called post increment or post decrement. In pre increment or pre decrement, the variable is incremented or decremented first and then other operations are performed on it whereas in the post increment or post decrement, the operations are performed first and then the variable is incremented or decremented. In the above example of post increment, the variable value 50 was assigned first and then it was incremented so the output was 50. And in the example of pre increment, the variable was incremented first so its value turned from 50 to 51 and then the new value 51 was assigned to the other variable. So the output was 51.
Post Views:
1,081