ITP102 Week 6 Asynchronous Student Activity
ITP102 Week 6 Asynchronous Student Activity
by:
JONATHAN BIEN C. DOMINGO
Subject Instructor
Objectives:
Introduction:
Operators
In java, there are different types of operators. These 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,
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.
is equivalent to,
count++;
int j = 3;
int k = 0;
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;
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.
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).
x1 op x2
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)
x1 x2 Result
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 if 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.
x1 x2 Result
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. 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.
^ (boolean logical exclusive OR)
x1 x2 Result
The result of an exclusive OR operation is TRUE, if and only if one operand is true and the other
is false. Note both operands must always be evaluated in order to calculate the result of an
exclusive OR.
! ( logical NOT)
The logical NOT takes in one argument, wherein that argument can be an expression, variable
or constant. Here is the truth table,
x1 Result
TRUE FALSE
FALSE TRUE
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, exp 2 is the value returned. If it is false, then exp3 is returned.
Input Grade
Grade < 75
Condition is True Condition is False
The following is the list of Java operators from highest to lowest precedence:
. [] ()
++ -- ! ~
* / %
+ -
== !=
& |
&&
||
?:
The highest precedence is on top row and the lowest is on the bottom row.
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;
Coding Guidelines
To avoid confusion in evaluating mathematical operations, keep your expressions simple and
use parenthesis.
Student Activity:
Instructions:
1. Below is a sample program code. Identify and categorize each operator used as Arithmetic,
Relational, or Logical.
int x = 10;
int y = 5;
int result = (x + y) * 2 – 3;
Boolean check = (x > y) and (y != 0);
Boolean final_result = (x % y == 0) or (x / y > 1) ;
2. Write your answers in a table with three columns: Operator, Type, and Explanation.
Instructions:
The following code contains syntax and logical errors related to operators. Identify and correct them.
int a = 20;
int b = 0 ;
1. Answer Activities cited above and save your working file in PDF format. If your solutions or
activity is done through written or hand-writing format, take a clear photo then convert it to
PDF format.
2. Save your file in your cloud storage account then share your file through link sharing and
submit it through https://forms.gle/b2JUDstyKZsPfW4q7
3. Deadline of submission will be on Friday, February 28, 2025 at 5:00PM.