0% found this document useful (0 votes)
3 views

ITP102 Week 6 Asynchronous Student Activity

The document is a reading material and review guide for ITP 102 – Programming 1, focusing on operators in programming. It covers arithmetic, relational, and logical operators, their syntax, functionality, and real-world applications, along with hands-on coding examples. Additionally, it includes student activities for identifying and correcting operator-related errors in code.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

ITP102 Week 6 Asynchronous Student Activity

The document is a reading material and review guide for ITP 102 – Programming 1, focusing on operators in programming. It covers arithmetic, relational, and logical operators, their syntax, functionality, and real-world applications, along with hands-on coding examples. Additionally, it includes student activities for identifying and correcting operator-related errors in code.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Student Reading Material and Review Guide

ITP 102 – Programming 1 (Logic Formulation)


Week 6

by:
JONATHAN BIEN C. DOMINGO
Subject Instructor

Subject Topic: Operators

Objectives:

• Understand the different types of operators in programming, including


arithmetic, relational, and logical operators.
• Learn the syntax and functionality of these operators in various
programming languages.
• Explore how arithmetic operators perform mathematical computations.
• Examine the role of relational operators in decision-making processes.
• Analyze the use of logical operators in conditional statements and control
flow.
• Discuss real-world applications of these operators in system development
and programming.
• Develop hands-on experience with operators through coding examples.

Introduction:

Operators are fundamental components in programming, allowing developers


to perform various computations and decision-making processes. Arithmetic
operators (+, -, *, /, %) enable calculations such as addition, subtraction,
multiplication, and division, essential in financial applications and data analysis.
Relational operators (>, <, >=, <=, ==, !=) compare values to determine conditions,
making them crucial for implementing security checks and user authentication
systems. Logical operators (&&, ||, !) help in combining multiple conditions, often
used in artificial intelligence and automated decision-making systems. These
operators play a vital role in software development, powering everything from simple
scripts to complex enterprise applications.
Supplemental Reading:

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,

Operator Use Description

* op1 * op2 Multiplies op1 by op2

/ op1 / op2 Divides op1 by op2

+ op1 + op2 Adds op1 and op2

- op1 – op2 Subtracts op2 from op1

% op1 % op2 Computes the remainder of dividing op1


by op2

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

Operator Use Description

++ op++ Increments op by 1; evaluates to the value of op before it


was incremented

++ ++op Increments op by 1; evaluates to the value of op after it


was incremented

-- op-- Decrements op by 1; evaluates to the value of op before


it was decremented

-- --op Decrements op by 1; evaluates to the value of op after it


was decremented
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 = 4 + 11 = 15

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.

Operator Use Description

> op1 > op2 op1 is greater than op2

>= op1 >= op2 op1 is greater than or equal to op2

< op1 < op2 op1 is less than op2

<= op1 <= op2 op1 is less than or equal to op2

== op1 == op2 op1 and op2 are equal

!= op1 != op2 op1 and op2 are not equal

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

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 &,

x1 x2 Result

TRUE TRUE TRUE

TRUE FALSE FALSE

FALSE TRUE FALSE

FALSE FALSE FALSE

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.

|| (logical OR) and |(boolean logical OR)

Here is the truth table for || and |,

x1 x2 Result

TRUE TRUE TRUE

TRUE FALSE TRUE

FALSE TRUE TRUE

FALSE FALSE FALSE

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)

Here is the truth table for ^,

x1 x2 Result

TRUE TRUE FALSE

TRUE FALSE TRUE

FALSE TRUE TRUE

FALSE FALSE FALSE

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.

Here is the flowchart of how ?: works,

Input Grade

Grade < 75
Condition is True Condition is False

Print Failed Print Passed


Operator Precedence

Operator precedence defines the compiler’s order of evaluation of operators so as to come


up with an unambiguous result.

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.

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;

Coding Guidelines

To avoid confusion in evaluating mathematical operations, keep your expressions simple and
use parenthesis.
Student Activity:

Activity 1: Identifying Operators in Code

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.

Activity 2: Correct the Errors

Instructions:
The following code contains syntax and logical errors related to operators. Identify and correct them.

int a = 20;
int b = 0 ;

int sum = a plus b;


is_greater = a > b && b != 0?
division = a / b if b ! 0 else "Undefined" ;
Tasks:

1. Rewrite the code correctly.


2. Explain the errors found in the original code.

Very important NOTE: General instruction for submission of student activity.

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.

You might also like