Part 2 : Calculating with numbers
Seoul National University of Science and Technology
Information Technology Management
Lecture slides index
March 19, 2025
+
, subtraction -
, multiplication *
, and division /
.*
and /
are calculated before those involving +
and -
, as is customary in elementary school mathematics.int calculationWithParentheses = (1 + 1) + 3 * (2 + 5);
System.out.println(calculationWithParentheses); // prints 23
int calculationWithoutParentheses = 1 + 1 + 3 * 2 + 5;
System.out.println(calculationWithoutParentheses); // prints 13
int calculationWithParentheses = (1 + 1);
System.out.println(calculationWithParentheses); // prints 2
calculationWithParentheses = calculationWithParentheses + 3 * (2 + 5);
System.out.println(calculationWithParentheses); // prints 23
int calculationWithoutParentheses = 1 + 1;
calculationWithoutParentheses = calculationWithoutParentheses + 3 * 2;
calculationWithoutParentheses = calculationWithoutParentheses + 5;
System.out.println(calculationWithoutParentheses); // prints 13
Sample 1
Sample 2
1 + 1 + 3 * 2 + 5
, which is evaluated prior to its assignment to the variable.int first = 2;
int second = 4;
System.out.println(first + second); // prints 6
System.out.println(2 + second - first - second); // prints 0
What does the following program print?
System.out.println
prints the value of a variable.+
.Example 1
Example 2
+
is a string, the other operand will be changed into a string too during program execution.2
is turned into the string “2”, and a string has been appended to it.Sample output 1
int x = 10;
System.out.println("The value of the variable x is: " + x);
int y = 5;
int z = 6;
System.out.println("y is " + y + " and z is " + z);
Output
double whenDividendIsFloat = 3.0 / 2;
System.out.println(whenDividendIsFloat); // prints 1.5
double whenDivisorIsFloat = 3 / 2.0;
System.out.println(whenDivisorIsFloat); // prints 1.5
Output
(double)
before it:int first = 3;
int second = 2;
double result1 = (double) first / second;
System.out.println(result1); // prints 1.5
double result2 = first / (double) second;
System.out.println(result2); // prints 1.5
double result3 = (double) (first / second);
System.out.println(result3); // prints 1.0
Output
The next exercises task you with calculating the average of the entered numbers. Let’s briefly review the concept of average.
An average refers to the sum of numbers divided by their count. For instance, the average of the numbers 5 and 3 can be calculated with the formula (5+3)/2. Similarly, the average of the numbers 1, 2, and 4 is produced by the formula (1+2+4)/3.
In the context of programming, there are a few things to keep in mind.
Example output
Example output 1
What does the following program print?
int dividend = 3;
int divisor = 2;
double result = dividend / divisor * 1.0;
System.out.println(result);
When a computer executes program code, it does it one command at a time, always advancing exactly as specified by the code.
When a value is assigned to a variable, the same chain of events always occurs: the value on the right-hand side of the equality sign is copied and assigned to the variable on the left-hand side (i.e., copied to the location specified by that variable).
It’s crucial for a programmer to understand that assigning a value to a variable always does the same thing.
In the following slides, there are three common misunderstandings related to assigning a value to a variable. Read it and understand it carefully.
first = second
has been executed, it’s often assumed that the value of the variable second
has been moved to the value of the variable first
, and that the variable second
no longer holds a value, or that its value is 0, for instance. This is incorrect, as executing first = second
means that the value in the position specified by second
is merely copied to the place specified by the variable first
. Hence, the variable second
is not modified.first = second
has been executed, it’s often assumed that any change in the value of the variable second
is automatically also reflected in the value of the variable first
. This is incorrect; assignment – i.e., copying – is a one-off event. It only occurs when the program code first = second
is executed.first = second
the value of the variable second
is set as the value of the variable first
. The confusion also manifests itself in situations where the programmer accidentally writes e.g. 42 = value
– fortunately, IDEs provide support on this issue too (we will start using IDEs soon).Don’t forget!
Analyze the execution of this code fragment using memory tables.
Don’t forget!
Computer Language