0% found this document useful (0 votes)
7 views6 pages

Module 6 Java Operators

This module covers Java Operators, which are essential for performing operations on variables and values in Java programming. Students will learn about different types of operators including arithmetic, assignment, comparison, and logical operators, and will be able to apply them in writing Java expressions and conditions. The module also differentiates between expressions and conditions, emphasizing their roles in programming logic.

Uploaded by

saringanervie6
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)
7 views6 pages

Module 6 Java Operators

This module covers Java Operators, which are essential for performing operations on variables and values in Java programming. Students will learn about different types of operators including arithmetic, assignment, comparison, and logical operators, and will be able to apply them in writing Java expressions and conditions. The module also differentiates between expressions and conditions, emphasizing their roles in programming logic.

Uploaded by

saringanervie6
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

DCIT 22 – Computer Programming 1

Module 6: Java Operators

Introduction:
In this module, students will students will explore Java Operators. Operators in Java
are used to perform various types of operations on variables and values, ranging from
arithmetic and assignment operations to logical and comparison operations. This module will
guide students through the different types of Java operators, providing an understanding of
how these operators influence the flow and functionality of a Java program. By the end of
this module, students will be able to write and understand Java expressions and conditions
that use these operators effectively.

Objectives:
In this module the student should be able to:
1. Identify and describe the different types of Java operators.
2. Apply assignment operators to set and update variable values.
3. Utilize comparison operators to make logical decisions in programs.
4. Implement logical operators to evaluate complex boolean expressions.
5. Differentiate between expressions and conditions in Java programming.

Java Operators
Java Operators are the symbols that are used to perform various operations on variables
and values. By using these operators, we can perform operations like addition, subtraction,
checking less than or greater than, etc.

Example:

Although the + operator is often used to add together two values, like in the example above, it
can also be used to add together a variable and a value, or a variable and another variable:

There are different types of operators in Java:


 Arithmetic Operators
 Assignment Operators
 Comparison Operators
 Logical Operators

Java Arithmetic Operators


These are used in mathematical expressions in the same way that they are used in algebra.
Operator Name Description Example
+ Addition Adds together two values x+y
- Subtraction Subtracts one value from another x–y
* Multiplication Multiplies two values x*y

Cavite State University – Naic


Information Technology Department
DCIT 22 – Computer Programming 1
Module 6: Java Operators

/ Division Divides one value by another x/y


% Modulus Returns the division remainder x%y
++ Increment Increases the value of a variable ++x
by 1
-- Decrement Decreases the value of a variable --x
by 1

Example for % (Modulus) operator:

Java Assignment Operators


Assignment operators are used to assign values to variables. In the example below,
we use the assignment operator (=) to assign the value 10 to a variable called x:

Operator Description Example


= Simple assignment operator. Assigns values C = A + B will assign value of
from right side operands to left side operand. A + B into C
+= Add AND assignment operator. It adds right C += A is equivalent to
operand to the left operand and assign the result to
C=C+A
left operand.
-= Subtract AND assignment operator. It subtracts C -= A is equivalent to
right operand from the left operand and assign C=C−A
the result to left operand.
*= Multiply AND assignment operator. It multiplies C *= A is equivalent to
right operand with the left operand and assign C=C*A
the result to left operand.
/= Divide AND assignment operator. It divides left C /= A is equivalent to
operand with the right operand and assign the C=C/A
result to left operand.
%= Modulus AND assignment operator. It takes C %= A is equivalent to
modulus using two operands and assign the C=C%A
result to left operand.

Cavite State University – Naic


Information Technology Department
DCIT 22 – Computer Programming 1
Module 6: Java Operators

Example using /= operator:

Java Comparison Operators


Comparison operators are used to compare two values (or variables). This is important in
programming, because it helps us to find answers and make decisions. The return value of a
comparison is either true or false. These values are known as Boolean values.

In the following example, we use the greater than operator (>) to find out if 5 is greater than
3:

Operator Name Description Example


== Equal to Checks if the values of two operands are x == y
equal or not, if yes then condition becomes
true.
!= Not equal Checks if the values of two operands are x != y
equal or not, if values are not
equal then condition becomes true.
> Greater than Checks if the value of left operand is greater x > y
than the value of right operand, if yes then
condition becomes true.
< Less than Checks if the value of left operand is less than x < y
the value of right operand, if yes then
condition becomes true
>= Greater than or Checks if the value of left operand is greater x >= y
equal to than or equal to the value of right operand, if yes
then condition becomes true.
<= Less than or Checks if the value of left operand is less than x <= y
equal to or equal to the value of right operand,
if yes then condition becomes true.

Cavite State University – Naic


Information Technology Department
DCIT 22 – Computer Programming 1
Module 6: Java Operators

Example code using == (equal to) and != (not equal to) operator:

Example code using >= (greater than or equal to) and <= (less than or equal to) operator

Java Logical Operators


Java Logical Operators are used to perform logical operations on boolean values (true or
false), often in conditional statements or control structures. They help in combining multiple
conditions and are particularly useful in decision-making. Logical operators are used to
determine the logic between variables or values:

Operator Name Description Example


&& Logical and Returns true if both operands are true. If either x < 5 &&
of the conditions is false, the entire x < 10
expression returns false
|| Logical or Returns true if at least one of the operands is x < 5 || x < 4
true. The expression returns false only if
both conditions are false.
! Logical not Returns true if the operand is false and false if !(x < 5 &&
the operand is true. It inverts the boolean x < 10)
value.

Example code using && (logical and) operator:

Cavite State University – Naic


Information Technology Department
DCIT 22 – Computer Programming 1
Module 6: Java Operators

Example code using || (logical or) operator:

Example using ! (logical not) operator:

Java Conditional Operator


Conditional operator is also known as the ternary operator. This operator consists of three
operands and is used to evaluate Boolean expressions. The goal of the operator is to
decide, which value should be assigned to the variable. The operator is written as –

variable x = (expression) ? value if true : value if false

Example code:

Cavite State University – Naic


Information Technology Department
DCIT 22 – Computer Programming 1
Module 6: Java Operators

‘Expressions’ and ‘Conditions’, How do they differ?


Expression:
 An expression is a combination of variables, operators, and values that a
programming language can evaluate to produce a result.
 Expressions can be used in calculations, assignments, or as arguments to functions.
The result of an expression is always a value (which could be a number, string, or
boolean).
 Examples of expressions:
o 5 + 3 (evaluates to 8)
o x * y (evaluates to the product of x and y)
o a > b (evaluates to true or false)
o "Hello, " + name (concatenates strings)

Condition:
 A condition is a specific type of expression that evaluates to a boolean value (true or
false). It is mainly used to make decisions in control structures like if, while, or for
loops (we will discuss these in later lessons).
 Conditions typically compare variables or values using comparison operators (like
==, !=, >, <, etc.) and can be combined using logical operators (&&, ||, !).
 Examples of conditions:
o age >= 18 (evaluates to true if age is 18 or older)
o score == 100 (evaluates to true if score equals 100)
o isLoggedIn && hasPermission (evaluates to true if both isLoggedIn and
hasPermission are true)

Key points:
Expressions can be any calculable statement that produces a value, while conditions are
specifically used for making decisions based on boolean outcomes. Also an expression can
result in any data type, but a condition always results in a Boolean.

References:

W3Schools. (n.d.). Java Operators. Retrieved November 2024, from


[Link]

Tutorials Point. (n.d.). Java Operators. Retrieved November 2024, from


[Link]

IBM. (March 03, 2021). Expressions and Conditions. Retrieved November 2024 from
[Link]

Cavite State University – Naic


Information Technology Department

You might also like