0% found this document useful (0 votes)
40 views32 pages

Object-Oriented Programming: Course Lecturer: Denis Ssebuggwawo, Phd. Course Tutor: Mr. Denis Gonzaga

This document discusses expressions and operators in Java. It covers arithmetic, assignment, relational, logical, and increment/decrement operators. Examples are provided for each operator type to demonstrate their usage. The course is taught by Denis Ssebuggwawo, PhD and Mr. Denis Gonzaga serves as the tutor.

Uploaded by

Denisho Dee
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views32 pages

Object-Oriented Programming: Course Lecturer: Denis Ssebuggwawo, Phd. Course Tutor: Mr. Denis Gonzaga

This document discusses expressions and operators in Java. It covers arithmetic, assignment, relational, logical, and increment/decrement operators. Examples are provided for each operator type to demonstrate their usage. The course is taught by Denis Ssebuggwawo, PhD and Mr. Denis Gonzaga serves as the tutor.

Uploaded by

Denisho Dee
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Object-Oriented Programming

Course Lecturer: Denis Ssebuggwawo, PhD.

Course Tutor: Mr. Denis Gonzaga


1

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

Object-Oriented Programming with Java

Expressions and Operators in Java Arithmetic operators: These include: + Addition Subtraction * Multiplication / Division % Modulus (Remainder)

Object-Oriented Programming with Java

Expressions and Operators in Java Arithmetic operators: Examples float courseWork, examMark, totalMark; totalMark = courseWork + examMark;

Object-Oriented Programming with Java

int x, n; x = n%4;
4

Expressions and Operators in Java Arithmetic operators: Examples int numberOfStudents; a) float sum, average; average = sum/numberOfStudents;

Object-Oriented Programming with Java

b) int a, b, c, d; d = a (b * c);
5

Expressions and Operators in Java Arithmetic operators:

Object-Oriented Programming with Java

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 =

Object-Oriented Programming with Java

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;

Object-Oriented Programming with Java

Expressions and Operators in Java Assignment Operators: =, +=, -=, *=, /=, %= Examples: c) int depValue, payBackValue; depValue = depValue payBackValue; OR depValue = payBackValue;

Object-Oriented Programming with Java

Expressions and Operators in Java Assignment Operators: =, +=, -=, *=, /=, %= Examples: d) float loan, interestRate; loan = loan * interestRate; OR loan * = interestRate;

Object-Oriented Programming with Java

10

Expressions and Operators in Java Assignment Operators: =, +=, -=, *=, /=, %= Examples: e) int newValue, m; newValue = newValue / m; OR newValue / = m;

Object-Oriented Programming with Java

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

Object-Oriented Programming with Java

Expressions and Operators in Java Equality and Relational Operators: Examples

Object-Oriented Programming with Java

b) char c; if (ch ! = c) System.out.printf(Enter more values\n); else System.out.printf(End of the input\n);


14

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;

Object-Oriented Programming with Java

15

Expressions and Operators in Java Equality and Relational Operators: Examples

Object-Oriented Programming with Java

d) float sum, num; if (sum <= 500) sum += num; else System.out.printf(\nEnd of input\n);

16

Expressions and Operators in Java Equality and Relational Operators: Examples

Object-Oriented Programming with Java

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.

Object-Oriented Programming with Java

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

Object-Oriented Programming with Java

|| !

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

Object-Oriented Programming with Java

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

Object-Oriented Programming with Java

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

Object-Oriented Programming with Java

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);

Object-Oriented Programming with Java

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

Object-Oriented Programming with Java

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

Object-Oriented Programming with Java

Expressions and Operators in Java Logical Operators: Note: (n < 1 || n > 12) is equivalent to: n >= 1 && n < 12)

Object-Oriented Programming with Java

! 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

++ --

increment operator: increment by 1 decrement operator: decrement by 1


27

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

You might also like