Object-Oriented Programming: Course Lecturer: Denis Ssebuggwawo, Phd. Course Tutor: Mr. Denis Gonzaga
Object-Oriented Programming: Course Lecturer: Denis Ssebuggwawo, Phd. Course Tutor: Mr. Denis Gonzaga
Expressions and Operators in Java Expressions in Java are formed by combining constants, variables and operators. The most commonly used operators in Java are:
-Arithmetic operators -Assignment operators -Relational operators -Logical operators -Increment and decrement operators
Expressions and Operators in Java Arithmetic operators: These include: + Addition Subtraction * Multiplication / Division % Modulus (Remainder)
Expressions and Operators in Java Arithmetic operators: Examples float courseWork, examMark, totalMark; totalMark = courseWork + examMark;
int x, n; x = n%4;
4
Expressions and Operators in Java Arithmetic operators: Examples int numberOfStudents; a) float sum, average; average = sum/numberOfStudents;
b) int a, b, c, d; d = a (b * c);
5
The usual precedence of operators apply. Thus: + and have the same precedence *, /, % have the same precedence *, /, % have a higher precedence than that of or +
6
Expressions and Operators in Java Assignment Operators: The assignment operators include: =, +=, -=, *=, /=, %= The basic assignment operator is =
It is used to assign an expression (on the right) to the variable on the left of =.
7
Expressions and Operators in Java Assignment Operators: =, +=, =, *=, /=, %= Examples: a) int x; x = x % 4; OR x % = 4; b) float sum; int num; sum = sum + num; OR sum + = num;
Expressions and Operators in Java Assignment Operators: =, +=, -=, *=, /=, %= Examples: c) int depValue, payBackValue; depValue = depValue payBackValue; OR depValue = payBackValue;
Expressions and Operators in Java Assignment Operators: =, +=, -=, *=, /=, %= Examples: d) float loan, interestRate; loan = loan * interestRate; OR loan * = interestRate;
10
Expressions and Operators in Java Assignment Operators: =, +=, -=, *=, /=, %= Examples: e) int newValue, m; newValue = newValue / m; OR newValue / = m;
11
Object-Oriented Programming with Java Expressions and Operators in Java Equality and Relational operators: These include: == equal to != not equal to > greater than < less than <= less than or equal to >= greater than or equal to
12
Expressions and Operators in Java Equality and Relational Operators: Examples a) int num1, num2; if (num1 == num2) System.out.printf(the numbers are equal\n); else System.out.printf(The numbers are different);
13
Expressions and Operators in Java Equality and Relational Operators: Examples c) int a,b, c; if (a < b) c = b c; else c = b a;
15
d) float sum, num; if (sum <= 500) sum += num; else System.out.printf(\nEnd of input\n);
16
e) char ch1, ch2; if (ch1 >= ch2) System.out.printf(\n ch2 comes before ch2);
17
Expressions and Operators in Java Equality and Relational Operators: Relational operators are used for comparisons.
The assignment a==b==c is wrong! The correct assignment is: (a==b) && (b==c) Where && is a logical operator (see below)
18
Expressions and Operators in Java Logical Operators: These include: && and
|| !
or Not
19
Expressions and Operators in Java Logical Operators: && (and) Logic using the truth table A T T F F B T F T F A && B T F F F
20
Expressions and Operators in Java Logical Operators: || (or) Logic using the truth table A T T F F B T F T F A || B T T T F
21
Expressions and Operators in Java Logical Operators: ! (not) Logic using the truth table A T T F F B T F T F !A F F T T !B F T F T
22
Expressions and Operators in Java Logical Operators: Examples a) char ch; if (ch >= a) && (ch <= z) System.out.printf(Enter character:\n); else System.out.printf(Not letter of the alphabet\n);
23
Expressions and Operators in Java Logical Operators: Examples b) int a; if (a < 0) || (a > 9) System.out.printf(Number is outside the range.); else System.out.printf(Number is within range.);
24
Expressions and Operators in Java Logical Operators: Examples c) int n; if !(n < 1 || n > 12) /* !((n<1 ) || (n > 12)) */ System.out.printf(Number is within range.); else System.out.printf(Number is outside the range.);
25
Expressions and Operators in Java Logical Operators: Note: (n < 1 || n > 12) is equivalent to: n >= 1 && n < 12)
! has a higher precedence than && which has a higher precedence than ||
26
Expressions and Operators in Java Increment and Decrement Operators: These include:
++ --
Object-Oriented Programming with Java Expressions and Operators in Java Increment and Decrement Operators: Examples: a) int a, n; char ch; n =5; if (ch ! = c) a =++n; /* ++n equiv. to n + 1 */ else a = n++; /* n++ equiv. to n + 1 */
28
Object-Oriented Programming with Java Expressions and Operators in Java Increment and Decrement Operators: Examples: b) int a, decreasingNumber; decreasingNumber = 10; if (decreasingNumber ! = 0) a = -- decreasingNumber; else a = decreasingNumber--;
29
Object-Oriented Programming with Java Expressions and Operators in Java Increment and Decrement Operators: Examples: In example a) above, ++n increments n before using its value, If n = 5; a =++n; first increments n (to 6) and then assigns the value 6 to a.
30
Object-Oriented Programming with Java Expressions and Operators in Java Increment and Decrement Operators: Examples: In example a) above, n++ increments after it has been used. If n = 5; a =n++; first assigns the value n (=5) to a and then increments n to 6
31
Object-Oriented Programming with Java Expressions and Operators in Java Increment and Decrement Operators: Examples: In both case, though, a is assigned the value 6
The same explanation applies to example b)
32