Module 05 - Expressions and Operators in Java
Module 05 - Expressions and Operators in Java
2
Table of Contents
COURSE DESCRIPTION 2
MISSION 2
VISION 2
MARIE CHERIE R. OCHEA 2
COURSE REQUIREMENTS 2
INTENDED LEARNING OUTCOMES 4
PRE-TEST: 4
Programming Cycle 10
Algorithm 11
Pseudocodes 12
Flowchart 13
Conditional Statements 14
Looping Constructs 16
Digital Electronics 17
POST-TEST: 18
ANSWER KEY: 24
Activity 25
3
COMPUTER
COURSE DESCRIPTION
2. Participations
3. Major Examinations
4
Republic of the Philippines
CAVITE STATE UNIVERSITY
Bacoor City Campus
SHIV, Molino VI, City of Bacoor
5
INTENDED LEARNING OUTCOMES
1. Familiarize with the hardware and software concepts of the computer system
2. Identify the steps involved in developing programs through the SDLC model
PRE-TEST:
Direction: Read each statement or question below carefully and choose the correct answer.
a. Problem Definition
b. Problem Analysis
c. Algorithm Development
2. A list of internally stored instructions.A programming cycle that looks at the problem itself;
a. Problem Definition
b. Problem Analysis
c. Algorithm Development
3. A programming cycle that outlines problems down into segments; strategy on how to do
the task.
a. Problem Definition
b. Problem Analysis
6
c. Algorithm Development
a. Problem Definition
b. Problem Analysis
c. Algorithm Development
5. A finite set of well-defined instructions for accomplishing a task which, given an initial
a. Algorithm
b. Flowcharts
c. Pseudocode
d. Syntax
a. Algorithm
b. Flowcharts
c. Pseudocode
d. Syntax
a. Algorithm
b. Flowcharts
c. Pseudocode
d. Syntax
a. Hexagon
b. Oval
c. Rectangle
d. Parallelogram
a. Hexagon
7
b. Oval
c. Rectangle
d. Parallelogram
a. Hexagon
b. Oval
c. Rectangle
d. Parallelogram
JAVA PROGRAM
● to compute values
● x = 5;
● y = x;
● z = x * y;
JAVA OPERATORS
Operators are used in Java to manipulate the values of variables. These follow a
set of rules for precedence and associativity so that the compiler will know which
operator to evaluate first in case multiple operators are used in one expression.
8
Operators are special symbols that perform some action on its operands and return
● Arithmetic
● Assignment
● Relational
● Logical
ARITHMETIC OPERATORS
Arithmetic operators are used in mathematical expressions in the same way that
they are used in Algebra. The following are the arithmetic operators:
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo
list that you might not be familiar with is the remainder operator. The remainder of
an operation is the remainder of the divisor divided by the dividend. In other words,
Note that when an integer and a floating-point number are used as operands to a
9
converted to a floating-point number before the operation takes place.
The ++ operator increments its single operand, which must be a variable, by one.
The behavior of this operator depends on its position relative to the operand.
The -- operator decrements its single numeric operand, which must be a variable,
by one. Like the ++ operator, the behavior of -- depends on its position relative to
the operand.
The increment and decrement operators can be placed before or after an operand.
When the increment and decrement operators are used before an operand, it
operand, the old value of the variable will be used in the expression where it
appears.
ASSIGNMENT OPERATORS
The simplest assignment operator is the standard equal (=) sign operator. This operator
assigns the value of the expression on the right side to the variable on the left side. For
10
example, x = y+3 evaluates the expression y+3 and assigns the result to x.
assignment operator can also be used to make a series of assignments in a single statement
as shown below:
x = y = z = 7;
7. The right side of an assignment expression is always calculated before the assignment
takes place. This makes it possible to use an expression statement as in the following code:
int x = 5; x = x + 2;
The other Java assignment operators are known as arithmetic assignment operators. These
operators provide a shortcut for assigning a value. When the previous value of a variable is a
factor in determining the value that you want to assign, the arithmetic assignment operators
Arithmetic assignment operators are best explained by example. For instance, the following
x += 5;
x = x + 5;
These operators instruct the interpreter to use the operand on the left (x in this case) as the
left-side operand in an expression with the right-side operand (5 in this case). The operator
that precedes the equals sign determines the operation performed between the two entities.
The result of this operation is then assigned to the operand on the left (x).
11
RELATIONAL OPERATORS
Boolean value (either true or false). Java provides the following six relational
operators:
== is equal to
!= is not equal to
All relational operators are binary. For example: Op1 == Op2 Op1 is equal to
Op2.
This requires that the two operands be of the same data type or data types that can
LOGICAL OPERATORS
operators, which are used for the logical combinations AND, OR, XOR, and logical
NOT.
For AND combinations, the & or && logical operators are used. When two Boolean
12
expressions are linked by the & or && operators, the combined expression returns a
This expression combines two comparison expressions: score > 75000 and
playerLives < 10. If both of these expressions are true, the value true is assigned to
the variable extraLife. In any other circumstance, the value false is assigned to the
variable.
OPERATOR PRECEDENCE
When more than one operator is used in an expression, Java has an established
many cases, this precedence determines the overall value of the expression.
y = 6 + 4 / 2;
The y variable receives the value 5 or the value 8, depending on which arithmetic
operation is handled first. If the 6 + 4 expression comes first, y will have the value of
5. Otherwise, y equals 8.
If two operations have the same precedence, the one on the left in the actual
To change the order in which expressions are evaluated, place parentheses around
13
the expressions that should be evaluated first. You can nest one set of parentheses
inside another to make sure that expressions are evaluated in the desired order; the
y = (6 + 4) / 2
The value of 5 is the result because 6 + 4 is calculated first, and then the result, 10, is
divided by 2.
JAVA STATEMENTS
Statements are the tasks you wish your program to do. Statements are your
expressions.
There are some statements that are ready to be used and all we have to do is to
statements are the basic building blocks of a program. Compound statements are
used to organize simple statements into complex structures, which are called control
structures since they control the order in which the statements are executed.
Decision control structures are Java that allows a programmer to select and
The decision control structure includes types such as If, If- Else, and If-Else-If. We
The if statement specifies that a statement (or block of code) will be executed if and
if (expression) statement;
expression). Indent the statements inside the if-block. Such indentation is optional but
structured programs.
If there is a need to execute several statements (compound statement), then left and
if (expression)
statement1; statement2;
15
}
whether the result of the conditional expression would be true or false. If the result is
Should the result of the condition be false, it skips the statement body and proceeds
Making a decision involves choosing between two alternative courses of action based
The if statement is the flowchart equivalent of the diamond symbol (decision box).
Recall that the diamond box has two possible outputs, one for a TRUE result and
The choice of path is dependent on the given condition placed on the decision box.
However, whether the result would be true or not, the path would always be
The concept of the decision box is always applied in programming, the if statement is
the actuality of the decision box where the if statement also contains conditions that
16
The statement in the else-clause of an if-else block can be another if-else structure.
Nested if structures are particularly useful when two conditions must be met before
some action is taken. The figure in the slide depicts the nested if structure.
When using nested if structure, you must be cautious on the placement of any else
clauses.
Another way you can add decision-making code to your programs is with the switch
a switch statement is more appropriate when you have many options based on the
value of a given expression, and on occasions when different sets of values require
The while loop is a statement or block of statements that is repeated as long as some
condition is satisfied. Depending on the Boolean expression, the loop may not be
executed at all.
appearance, as the condition appears after the body. The statements inside a do-
A do-while loop allows statements inside the curly braces {} to be executed at least
17
once. The minimum number of iterations is one and the maximum number is infinity.
A common programming error when using the do-while loop is forgetting to write the
semi-colon after the while expression. Just like in while loops, make sure that your
A for loop instructs a program to perform a block of code a specified number of times.
There are many applications for a for loop, including tasks such as reading through a
BRANCHING STATEMENTS
Branching statements allow you to redirect the flow of program execution. These
were commonly used during the time when programs follow a linear path, i.e., as a
single line). To control program flow, programs then had to jump around instructions.
convenient paths while maintaining the “structured” property of the program. Note
that structured programming requires modularity of the program, and that each
branching statements:
● break
● continue
● return
The break statement is used to “break out” of a loop. With loops, break can be used
to force an early exit from the loop, or to implement a loop with a test to exit in the
18
middle of the loop body. A break within a loop should always be protected within an if
The continue statement is similar to break, but less commonly used. A continue
statement when executed skips any remaining statements in the body of the structure
and proceeds with the test for the next iteration of the loop. It may occur only in a
The return statement is used to exit from the current method. The flow of control
POST-TEST:
Direction: Read each statement or question below carefully and choose the correct answer.
a. Line comment
b. JavaDoc
c. Block comment
2. Starts with two forward slashes (//) and ends at the end of the line the slashes appear on.
a. Line comment
b. JavaDoc
c. Block comment
19
d. None of the Above
a. Line comment
b. JavaDoc
c. Block comment
4. Words that are used by the programmer to properly identify or name a class, procedure,
method, or variables.
a. Identifiers
b. Literals
c. Data Types
d. Keywords
a. Identifiers
b. Literals
c. Data Types
d. Keywords
a. Identifiers
b. Literals
c. Data Types
d. Keywords
a. Identifiers
b. Literals
c. Data Types
d. Keywords
8. A data type that is one-bit wide and takes on the values true or false.
a. Integer
b. String
20
c. Boolean
d. Float
a. Integer
b. String
c. Boolean
d. Float
a. Integer
b. String
c. Boolean
d. Float
ANSWER KEY:
PRE-TEST POST-TEST
IDENTIFICATION: IDENTIFICATION:
1. B 1. B
2. A 2. A
3. B 3. B
4. A 4. A
5. C 5. C
6. B 6. B
7. C 7. C
8. C 8. C
9. A 9. A
10. D 10. D
21
ACTIVITY
Problem Analyzation:
1. Answer the following given problems and illustrate the correct flowcharts in any clean
paper.
3. Take a photo of your answer and attach it here in the google classroom..
4. Avoid copying the works of your classmates! Please answer it on your own.
22