What does the following assignment statement statement do:
sum = 42 - 12 ;
sum
.Normally you would say:
The statement puts 30 into sum
.
However, this is a little sloppy. Sometimes when the expression is complicated you need to think carefully about the two steps.
An expression is a combination of literals, operators, variable names, and parentheses used to calculate a value.
This (slighly incomplete) definition needs some explanation:
+
or times *
that asks for an arithmetic operation.(
and )
.This might sound awful. Actually, this is stuff that you know from algebra, like:
(32 - y) / ( x + 5 )
In the above, the character /
means division.
Not just any mess of symbols will work.
The following
32 - y) / ( x 5 + )
is not a syntactically correct expression.
There are rules for this, but the best rule is that an expression
must look OK as algebra.
However, multiplication must always be shown by using a *
operator.
You can't multiply two variables by placing them next to each other.
So, although xy
might be correct in algebra,
you must use x*y
in Java.
Which of the following expressions are correct? (Assume that the variables have been properly declared elsewhere.)
Expression | 53 | 12 - 3) | x + 34 | *z 99 | sum + value |
---|---|---|---|---|---|
Correct or Not? |