-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignmentOperators.java
More file actions
51 lines (31 loc) · 1.7 KB
/
Copy pathAssignmentOperators.java
File metadata and controls
51 lines (31 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package lesson2OperatorsandFlowControl;
public class AssignmentOperators {
public static void main(String[] args) {
int num;
String name;
num = 15;
name = "J";
System.out.println("num is assigned: " + num);
System.out.println("name is assigned: " + name);
/* += operator - operates by adding current value of variable on the left to the value on right and then assign result
* to the operand on left
*/
int num1 = 20, num2 = 1; // the three lines here are necessary for declaring variables to use each operator
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
num1 += num2; // Adds and assigns values
System.out.println("num1 = " + num1); // displays assigned values
// -= operator subtracts variable's value on right from current value on the left and then assigns the result to operand on left
num1 -= num2;
System.out.println("num1 = " + num1);
// *= operator multiplies current variable value on the left to the value on the right, and assign result to the left operand
num1 *= num2;
System.out.println("num1 = " + num1);
// /= operator divides current variable value on the left by the value on the right, then assigns quotient to left operand
num1 *= num2;
System.out.println("num1 = " + num1);
// %= operator divides current variable value on the left by the value, then assigns remainder to the left operand
num1 %= num2;
System.out.println("num1 = " + num1);
}
}