FIT1051 Chapter Notes - Chapter 4: Memory Address, Semicolon, Control Flow

39 views12 pages
Week 4
Wednesday, 14 March 2018
5:33 PM
Operators, Expressions, Statements
Operators
Data values can appear as literals in program code or be stored in variables or constants. Data value
can also be returned by method calls.
All these sources of data values can be combined as terms into expressions using operators.
Operators are special symbols that operate on their operands and return a result.
Ex. The expression 12* 2 returns 24 because he multiplication operator (*) has multiplied (the
operation) its two operands (12 and 2) to return a result (24).
Expressions evaluate to a single value which like all values in Java must have a data type. The
resulting data type depends on the data types of the terms of the expression and the operation
performed during its evaluation.
We can use expressions to:
Perform arithmetic calculations
Perform String manipulations
Assign values to variables (yes, "=" is an operator)
Compare data values
o These kinds of expressions return the boolean values true or false
o Expressions to control the sequence of statement execution
When there is more than one operator in an expression the question arises as to which operates first
i.e. which operator has precedence.
The higher the operator on the list, the higher the precedence. Operators with high
precedence's operates first compared to ones with lower precedence's.
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 12 pages and 3 million more documents.

Already have an account? Log in
Assignment Operator
An assignment statement is just an assignment expression terminated by a semi-colon.
It has the lowest precedence of all operators which is obvious.
o RHS expression must fully evaluate before its value can be stored at the LHS target
memory location
Operators return values, so the assignment operator returns its evaluated RHS.
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 12 pages and 3 million more documents.

Already have an account? Log in
Integer Division and Floating Point Division
If both operands of a division operator (/) are ints the operation is integer division where the return
value is the results as a truncated integer.
If one operand is double and the other is an int then a copy of the int's value will be auto-promoted
to a double as usual allowing floating point division to occur.
If both operands are ints you only need to cast one to double and auto promotion of the other
operand will cause floating point division to occur.
Compound Operators
The following two statements are equivalent:
totalSales = totalSales + thisSale;
totalSales += thisSale;
This compound operator syntax is valid for +,-,* and /.
This syntax is often used when accumulating a sum
o i.e. adding individual values to a total value
Used when accumulating a String
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 12 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Data values can appear as literals in program code or be stored in variables or constants. Data value can also be returned by method calls. All these sources of data values can be combined as terms into expressions using operators. Operators are special symbols that operate on their operands and return a result. The expression 12* 2 returns 24 because he multiplication operator (*) has multiplied (the operation) its two operands (12 and 2) to return a result (24). Expressions evaluate to a single value which like all values in java must have a data type. The resulting data type depends on the data types of the terms of the expression and the operation performed during its evaluation. When there is more than one operator in an expression the question arises as to which operates first i. e. which operator has precedence. The higher the operator on the list, the higher the precedence.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents