This material is licensed under the Creative Commons BY-NC-SA license, which means that you can use it and distribute it freely so long as you do not erase the names of the original authors. If you make changes in the material and want to distribute this altered version of the material, you have to license it with a similar free license. The use of the material for commercial use is prohibited without a separate agreement.
Authors: Arto Hellas, Matti Luukkainen
Translators to English: Emilia Hjelm, Alex H. Virtanen, Matti Luukkainen, Virpi Sumu, Birunthan Mohanathas, Etiënne Goossens
Extra material added by: Etiënne Goossens, Maurice Snoeren, Johan Talboom
The course is maintained by Technische Informatica Breda
Because changing the value of a variable is a very common operation, Java has special assignment operations for it.
When performing the assignment operation on an existing variable, it is written as variable operation= change
, for example variable += 5
. Note that a variable must be defined before you can assign a value to it. Defining a variable is done by specifying the variable type and the name of the variable.
The following example will not work because the type of the variable length
has not been defined.
length = length + 100; // error!
length += 100; // error!
When the type is defined, the operations will also work.
There are also other assignment operations:
Often during a loop, the value of a variable is calculated based on repetition. The following program calculates 3*4 somewhat clumsily as the sum 3+3+3+3:
In the beginning result = 0
. During the loop, the value of the variable is incremented by 3 on each iteration. Because there are 4 iterations, the value of the variable is 3*4 in the end.
Using the assignment operator introduced above, we can achieve the same behavior as follows:
Exercise assignment-shorthand-1: The sum of a set of numbers
Create a program that calculates the sum 1+2+3+…+n where n is a number entered by the user.
Example outputs:
Until what? ~~3~~ Sum is 6
The calculation above was: 1+2+3 = 6.
Until what? ~~7~~ Sum is 28
The calculation above was: 1+2+3+4+5+6+7 = 28.
Hint: Create the program using the while statement. Use a helper variable in your program to remember how many times the block has been executed. Use also another helper variable to store the sum. During each execution add the value of the helper variable that counts the executions to the variable in which you should collect the sum.
Exercise assignment-shorthand-2: The sum between two numbers
Similar to the previous exercise, except that the program should ask for both the lower and upper bound. You can assume that the users first gives the smaller number and then the greater number.
First: ~~3~~ Last: ~~5~~ The sum is 12
First: ~~2~~ Last: ~~8~~ The sum is 35
Exercise assignment-shorthand-3: Factorial
Create a program that calculates the factorial of the number n. The factorial n! is calculated using the formula 1×2×3×…×n. For example 4! = 1×2×3×4 = 24. Additionally, it is defined that 0! = 1.
Example outputs:
Type a number: ~~3~~ Factorial is 6
Type a number: ~~10~~ Factorial is 3628800
Exercise assignment-shorthand-4: Sum of the powers
Create a program that calculates the sum of \(2^0+2^1+2^2+...+2^n\), where n is a number entered by the user. The notation 2i means raising the number 2 to the power of i, for example \(2^4 = 2*2*2*2 = 16\). In Java we cannot write \(a^b\) directly, but instead we can calculate the power with the command
Math.pow(number, power)
. Note that the command returns a number of double type (i.e. floating point number). A double can be converted into the int type (i.e. whole number) as follows:int result = (int)Math.pow(2, 3)
. This assigns the value of \(2^3\) to variable result.Example outputs:
Type a number: ~~3~~ The result is 15
Type a number: ~~7~~ The result is 255
```