0% found this document useful (0 votes)
16 views14 pages

Comprog1 W Module 4

University of Makati ComProg Module

Uploaded by

Marq Francis
Copyright
© © All Rights Reserved
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)
16 views14 pages

Comprog1 W Module 4

University of Makati ComProg Module

Uploaded by

Marq Francis
Copyright
© © All Rights Reserved
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/ 14

COMPUTER

PROGRAMMING

WEEK 4 - MODULE

PROF. ROEL C. TRABALLO

Compiled by: Prof. ROEL C. TRABALLO


University of Makati – College of Computer Science
SECTION 4
INTRODUCTION TO JAVA PROGRAMMING
CHAPTER 2

Operators
In Java, there are different types of operators. There are arithmetic operators, relational
operators, logical operators and conditional operators. These operators follow a certain kind of
precedence so that the compiler will know which operator to evaluate first in case multiple
operators are used in one statement.

 Arithmetic operators
Here are the basic arithmetic operators that can be used in creating your Java
programs,

Compiled by: Prof. ROEL C. TRABALLO


University of Makati – College of Computer Science
Here's a sample program in the usage of these operators:
public class ArithmeticDemo {
public static void main(String[] args) {
//a few numbers
int i = 37;
int j = 42;
double x = 27.475;
double y = 7.22;
System.out.println("Variable values...");
System.out.println(" i = " + i);
System.out.println(" j = " + j);
System.out.println(" x = " + x);
System.out.println(" y = " + y);
//adding numbers
System.out.println("Adding...");
System.out.println(" i + j = " + (i + j));
System.out.println(" x + y = " + (x + y));
//subtracting numbers
System.out.println("Subtracting...");
System.out.println(" i - j = " + (i - j));
System.out.println(" x - y = " + (x - y));
//multiplying numbers
System.out.println("Multiplying...");
System.out.println(" i * j = " + (i * j));
System.out.println(" x * y = " + (x * y));
//dividing numbers
System.out.println("Dividing...");
System.out.println(" i / j = " + (i / j));
System.out.println(" x / y = " + (x / y));
//computing the remainder resulting from dividing numbers
System.out.println("Computing the remainder...");
System.out.println(" i % j = " + (i % j));
System.out.println(" x % y = " + (x % y));
//mixing types
System.out.println("Mixing types...");
System.out.println(" j + y = " + (j + y));
System.out.println(" i * x = " + (i * x));
}
}

Compiled by: Prof. ROEL C. TRABALLO


University of Makati – College of Computer Science
Here is the output of the program,
Variable values...
i = 37
j = 42
x = 27.475
y = 7.22
Adding...
i + j = 79
x + y = 34.695
Subtracting...
i - j = -5
x - y = 20.255
Multiplying...
i * j = 1554
x * y = 198.37
Dividing...
i/j=0
x / y = 3.8054
Computing the remainder...
i % j = 37
x % y = 5.815
Mixing types...
j + y = 49.22
i * x = 1016.58

Note: When an integer and a floating-point number are used as operands to a single
arithmetic operation, the result is a floating point. The integer is implicitly converted to a
floating-point number before the operation takes place.

 Increment and Decrement operators


Aside from the basic arithmetic operators, Java also includes a unary increment
operator (++) and unary decrement operator (--). Increment and decrement operators
increase and decrease a value stored in a number variable by 1.

For example, the expression,


count = count + 1; //increment the value of count by 1
is equivalent to,
count++;

Compiled by: Prof. ROEL C. TRABALLO


University of Makati – College of Computer Science
The increment and decrement operators can be placed before or after an operand. When
used before an operand, it causes the variable to be incremented or decremented by 1, and then
the new value is used in the expression in which it appears.

For example,
int i = 10,
int j = 3;
int k = 0;

k = ++j + i; //will result to k = 4+10 = 14

When the increment and decrement operators are placed after the operand, the old value
of the variable will be used in the expression where it appears.

For example,
int i = 10,
int j = 3;
int k = 0;

k = j++ + i; //will result to k = 3+10 = 13

 Relational operators
Relational operators compare two values and determines the relationship between
those values. The output of evaluation are the boolean values true or false.

Compiled by: Prof. ROEL C. TRABALLO


University of Makati – College of Computer Science
Here's a sample program that uses relational operators,
public class RelationalDemo {
public static void main(String[] args) {
//a few numbers
int i = 37;
int j = 42;
int k = 42;
System.out.println("Variable values...");
System.out.println(" i = " + i);
System.out.println(" j = " + j);
System.out.println(" k = " + k);
//greater than
System.out.println("Greater than...");
System.out.println(" i > j = " + (i > j)); //false
System.out.println(" j > i = " + (j > i)); //true
System.out.println(" k > j = " + (k > j)); //false
//greater than or equal to
System.out.println("Greater than or equal to...");
System.out.println(" i >= j = " + (i >= j)); //false
System.out.println(" j >= i = " + (j >= i)); //true
System.out.println(" k >= j = " + (k >= j)); //true
//less than
System.out.println("Less than...");
System.out.println(" i < j = " + (i < j)); //true
System.out.println(" j < i = " + (j < i)); //false
System.out.println(" k < j = " + (k < j)); //false
//less than or equal to
System.out.println("Less than or equal to...");
System.out.println(" i <= j = " + (i <= j)); //true
System.out.println(" j <= i = " + (j <= i)); //false
System.out.println(" k <= j = " + (k <= j)); //true
//equal to
System.out.println("Equal to...");
System.out.println(" i == j = " + (i == j)); //false
System.out.println(" k == j = " + (k == j)); //true
//not equal to
System.out.println("Not equal to...");
System.out.println(" i != j = " + (i != j)); //true
System.out.println(" k != j = " + (k != j)); //false
}
}

Compiled by: Prof. ROEL C. TRABALLO


University of Makati – College of Computer Science
Here's the output from this program:
Variable values...
i = 37
j = 42
k = 42
Greater than...
i > j = false
j > i = true
k > j = false
Greater than or equal to...
i >= j = false
j >= i = true
k >= j = true
Less than...
i < j = true
j < i = false
k < j = false
Less than or equal to...
i <= j = true
j <= i = false
k <= j = true
Equal to...
i == j = false
k == j = true
Not equal to...
i != j = true
k != j = false

 Logical operators
Logical operators have one or two boolean operands that yield a boolean result.
There are six logical operators: && (logical AND), & (boolean logical AND), || (logical
OR), | (boolean logical inclusive OR), ^ (boolean logical exclusive OR), and ! (logical
NOT).

The basic expression for a logical operation is,


x1 op x2

Compiled by: Prof. ROEL C. TRABALLO


University of Makati – College of Computer Science
where x1, x2 can be boolean expressions, variables or constants, and op is either
&&, &, ||, | or ^ operator. The truth tables that will be shown next, summarize the result
of each operation for all possible combinations of x1 and x2.

 && (logical AND) and & (boolean logical AND)


Here is the truth table for && and &,

The basic difference between && and & operators is that && supports short-
circuit evaluations (or partial evaluations), while & doesn't. What does this mean?

Given an expression,
exp1 && exp2
&& will evaluate the expression exp1, and immediately return a false
value is exp1 is false. If exp1 is false, the operator never evaluates exp2 because
the result of the operator will be false regardless of the value of exp2. In contrast,
the & operator always evaluates both exp1 and exp2 before returning an answer.

Here's a sample source code that uses logical and boolean AND,

public class TestAND {


public static void main( String[] args ) {
int i = 0;
int j = 10;
boolean test= false;
//demonstrate &&
test = (i > 10) && (j++ > 9);
System.out.println(i);
System.out.println(j);
System.out.println(test);
//demonstrate &
test = (i > 10) & (j++ > 9);
System.out.println(i);
System.out.println(j);
System.out.println(test);
}
}

Compiled by: Prof. ROEL C. TRABALLO


University of Makati – College of Computer Science
The output of the program is,
0
10
false
0
11
false

Note, that the j++ on the line containing the && operator is not evaluated since
the first expression (i>10) is already equal to false.

 || (logical OR) and | (boolean logical inclusive OR)


Here is the truth table for || and |,

The basic difference between || and | operators is that || supports short-circuit


evaluations (or partial evaluations), while | doesn't. What does this mean?

Given an expression,
exp1 || exp2
|| will evaluate the expression exp1, and immediately return a true value is exp1 is
true. If exp1 is true, the operator never evaluates exp2 because the result of the operator
will be true regardless of the value of exp2. In contrast, the | operator always evaluates
both exp1 and exp2 before returning an answer.

Here's a sample source code that uses logical and boolean OR,
public class TestOR {
public static void main( String[] args ) {
int i = 0;
int j = 10;
boolean test= false;
//demonstrate ||
test = (i < 10) || (j++ > 9);
System.out.println(i);
System.out.println(j);
System.out.println(test);
//demonstrate |
test = (i < 10) | (j++ > 9);
System.out.println(i);
System.out.println(j);
System.out.println(test);
}
}

Compiled by: Prof. ROEL C. TRABALLO


University of Makati – College of Computer Science
The output of the program is,
0
10
true
0
11
True
Note, that the j++ on the line containing the || operator is not evaluated since the
first expression (i<10) is already equal to true.

 ^ (boolean logical exclusive OR)


Here is the truth table for ^,

The result of an exclusive OR operation is TRUE, if and only if one operand is


true and the other is false. Note that both operands must always be evaluated in order to
calculate the result of an exclusive OR.

Here's a sample source code that uses the logical exclusive OR operator,
public class TestXOR {
public static void main( String[] args ) {
boolean val1 = true;
boolean val2 = true;
System.out.println(val1 ^ val2);
val1 = false;
val2 = true;
System.out.println(val1 ^ val2);
val1 = false;
val2 = false;
System.out.println(val1 ^ val2);
val1 = true;
val2 = false;
System.out.println(val1 ^ val2);
}
}

Compiled by: Prof. ROEL C. TRABALLO


University of Makati – College of Computer Science
The output of the program is,
false
true
false
true

 ! (logical NOT)
The logical NOT takes in one argument, wherein that argument can be an
expression, variable or constant.
Here is the truth table for !,

Here's a sample source code that uses the logical NOT operator,

public class TestNOT {


public static void main( String[] args ) {
boolean val1 = true;
boolean val2 = false;
System.out.println(!val1);
System.out.println(!val2);
}
}

The output of the program is,


false
true

 Conditional Operator (?:)


The conditional operator ?: is a ternary operator. This means that it takes in three
arguments that together form a conditional expression.
The structure of an expression using a conditional operator is,
exp1?exp2:exp3
wherein exp1 is a boolean expression whose result must either be true or false. If
exp1 is true, exp2 is the value returned. If it is false, then exp3 is returned.

Compiled by: Prof. ROEL C. TRABALLO


University of Makati – College of Computer Science
For example, given the code,
public class ConditionalOperator {
public static void main( String[] args ) {
String status = "";
int grade = 80;
//get status of the student
status = (grade >= 60) ? "Passed" : "Fail";
//print status
System.out.println( status );
}
}

The output of this program will be,


Passed

Here is the flowchart of how ?: works,

Here is another program that uses the ?: operator,


class ConditionalOperator {
public static void main( String[] args ) {
int score = 0;
char answer = 'a';
score = (answer == 'a') ? 10 : 0;
System.out.println("Score = " + score );
}
}

Compiled by: Prof. ROEL C. TRABALLO


University of Makati – College of Computer Science
The output of the program is,
Score = 10

 Operator Precedence
Operator precedence defines the compiler’s order of evaluation of operators so as
to come up with an unambiguous result.

Given a complicated expression,


6%2*5+4/2+88-10

we can re-write the expression and place some parenthesis base on operator
precedence,
((6%2)*5)+(4/2)+88-10;


References:
 JEDI Course Notes



Compiled by: Prof. ROEL C. TRABALLO


University of Makati – College of Computer Science
ASSESSMENT
NAME: _____________________________________________ Score: __________
Section: _________________ Date Submitted: _____________

K-W-L (KNOW, WANT TO KNOW, LEARNED)


K-W-L charts are graphic organizers that help students organize information before, during, and after a unit or a
lesson. They can be used to engage students in a new topic, activate prior knowledge, share unit objectives, and
monitor students’ learning.

1. Fill out column 1


Students shall respond to the first prompt in column 1: What do you know about the different learning
platforms? How and where did you know about those?
2. Fill out column 2
Students shall respond to the prompt in column 2: What do you want to know about this topic? Write down the
specific questions in which you are more interested.
3. Leave the last column blank: What did you learn? This must be filled out by the end of the week or after
discussion.

Compiled by: Prof. ROEL C. TRABALLO


University of Makati – College of Computer Science

You might also like