Week 2 Arithmetic Operators.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

Arithmetic Operators and Constants Terminology • Operators are used to perform an action • Operands are the data on which the action is performed • An expression is a statement made up of operators and operands • For example: Expression: Statement: Operators: Operands: balance = balance - 100 ba...

Arithmetic Operators and Constants Terminology • Operators are used to perform an action • Operands are the data on which the action is performed • An expression is a statement made up of operators and operands • For example: Expression: Statement: Operators: Operands: balance = balance - 100 balance = balance - 100; = balance 100 Assignment operator = • The assignment operator is used to assign a value to a variable. • When evaluating an expression, read from right to left. • For example: double balance; //declare a variable balance = 100.0; //assign a value to variable balance = balance + 20; //???? Arithmetic Operators • The arithmetic operators are used to perform operations on variables Operation Java Operator Addition + Subtraction - Multiplication * Division / Modulus (remainder) % Addition operator + • The addition operator (+) is used with number variables to add those values together. • The result of an addition may be stored in a variable. • For example: double balance = 100.0; double interest = 12.42; balance = balance + interest; Program to add two numbers public class Add2Numbers { public static void main(String[ ] args) { int no1; //declare variables int no2; int result; no1= 2; //assign values no2= 3; result = no1 + no2; //perform calc System.out.print(result); //display result } } Concatenation operator + • The + symbol used as addition operator when used with numeric values or variables • used as concatenation operator when used with strings – to concatenate strings together. (Concatenation operator – overloaded operator) • This is very useful in System.out.print() statements. • For example: balance = balance + interest; System.out.print("Balance: " + balance); Program to add two numbers public class Add2Numbers { public static void main(String[ ] args) { int no1 = 2; Addition operator int no2 = 3; int result = 0; result = no1 + no2; System.out.print ("The result is " + result); } } Concatenation operator Subtraction operator • The subtraction operator is used with variables to subtract one value from the other. • Again, the result of a subtraction may be stored in a variable. • For example: double balance = 100.0; double bankCharge = 1.42; balance = balance - bankCharge ; Multiplication operator * • The multiplication operator is used with variables to multiply those values together. • For example: double balance = 100.0; double interest = 0; double interestRate = 0.05; interest = balance * interestRate; balance = balance + interest; Division operator / • Important difference between integer division and decimal (floating point) division • If both operands are integers, the result is an integer. The remainder is disregarded 7 / 4 yields 1 • If one or both operands are real numbers (double), the result is a double. 7.0 / 4 yields 1.75 Division operator / • The division operator is used with variables to divide one value by another. • For example: int mathsMark = 75; int progMark = 58; int average; average = (mathsMark + progMark) / 2; What value is assigned to average? Division operator / • When used with int variables, the division operator will disregard any remainder. • For example: int answer; answer = 11 / 4; • For example: double answer; answer = 11 / 4.0; // answer is 2 // answer is 2.75 Modulus operator % • The modulus operator (%) is used with int variables to find the remainder after division. • For example: int answer; answer = 7 % 4; // answer is 3 • The modulus operator (%) returns the remainder after integer division Modulus operator % Examples of the modulus operator in Java Expression 29 % 9 Value 2 6%8 6 40 % 40 0 10 % 2 0 Modulus operator example A film last 132 minutes. How long is this in hours and minutes? int hours; int mins; int filmLength; //declare variables filmLength = 132; hours = filmLength /60; mins = filmLength % 60; //assign values Operator precedence • The multiplication, division, and modulus (*, /, %) operators take precedence over the addition and subtraction (+, -) operators. • For example: int answer; answer = 5 + 2 * 2 // answer is 9 Operator precedence • Use brackets to override operator precedence. Expressions in brackets are always evaluated first. • For example: int answer; answer = (5 + 2) * 2; //answer is 14 • The Assignment operator (=) has the lowest level of precedence, so it is always evaluated last Associativity of arithmetic operators Precedence / % * have higher precedence than + -, i.e. 3 + 2 * 5 gives 13 Associativity – if operators have same precedence then look at associativity – Arithmetic operators work from left to right i.e. 13 % 2 * 5 gives 5 – Associativity of assignment operator is right to left

Use Quizgecko on...
Browser
Browser