Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Module 05 - Expressions and Operators in Java

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Module 05 - Expressions and Operators in Java

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Republic of the Philippines

CAVITE STATE UNIVERSITY


Bacoor City Campus
SHIV, Molino VI, City of Bacoor

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

NC-DCIT 22: COMPUTER PROGRAMMING I

Republic of the Philippines Development of applications using web, mobile, and


CAVITE STATE UNIVERSITY emerging technologies with emphasis on
Bacoor City Campus requirements management, interface design,
SHIV, Molino VI, City of Bacoor usability, testing, deployment, including ethical and
legal considerations.

MISSION PROGRAM OUTCOMES ADDRESSED BY THE


Cavite State University shall provide COURSE. AFTER COMPLETING THIS COURSE, THE
excellent, equitable and relevant STUDENTS MUST BE ABLE TO:
educational opportunities in the arts,
science and technology through
quality instruction and relevant 1. Attain the vision, mission, goals and
research and development activities.
objectives of the university, campus and
It shall produce professional, skilled
and morally upright individuals for department,
global competitiveness. 2. Deliver a gender fair and gender sensitive
instruction to students aligned with University
goals and objectives,
3. Present necessary concepts, techniques, and
VISION process to provide the skills necessary to
The premier university in historic define and analyze the effective
Cavite recognized for excellence in communication in organization,
the development of globally 4. Show how to collect and structure information
competitive and morally upright in the development of requirements and
individuals. specifications,
5. Develop skills for effective interpersonal
communication to develop consensus using
classical techniques as well as computer
facilitated groupware to demonstrate and
analyze small group dynamics as related to
working with users.
COURSE REQUIREMENTS
1. Quizzes / Activities

2. Participations

3. Major Examinations

MARIE CHERIE R. OCHEA


Instructor, Embedded Systems
mariecherie.ochea@cvsu.edu.ph

4
Republic of the Philippines
CAVITE STATE UNIVERSITY
Bacoor City Campus
SHIV, Molino VI, City of Bacoor

5
INTENDED LEARNING OUTCOMES

After the completion of the unit, students will be able to:

1. Familiarize with the hardware and software concepts of the computer system

2. Identify the steps involved in developing programs through the SDLC model

3. Define what algorithm and pseudocode are

4. Identify the different symbols used in flowcharting

5. Describe the concepts of using conditional statements and looping constructs

6. Recognize the use of Java as a programming language

7. Learn the language elements in Java

8. Use array in a Java program

PRE-TEST:

Direction: Read each statement or question below carefully and choose the correct answer.

1. A programming cycle that specifies what we want the computer to do.

a. Problem Definition

b. Problem Analysis

c. Algorithm Development

d. Coding and Debugging

2. A list of internally stored instructions.A programming cycle that looks at the problem itself;

point of view of the computer; start with the input/output.

a. Problem Definition

b. Problem Analysis

c. Algorithm Development

d. Coding and Debugging

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

d. Coding and Debugging

4. A programming cycle that processes errors.

a. Problem Definition

b. Problem Analysis

c. Algorithm Development

d. Coding and Debugging

5. A finite set of well-defined instructions for accomplishing a task which, given an initial

state, will result in a corresponding recognizable end-state.

a. Algorithm

b. Flowcharts

c. Pseudocode

d. Syntax

6. A shorthand way of describing a computer program.

a. Algorithm

b. Flowcharts

c. Pseudocode

d. Syntax

7. A graphical representation of a sequence of operations.

a. Algorithm

b. Flowcharts

c. Pseudocode

d. Syntax

8. A flowchart that are called Terminal Blocks

a. Hexagon

b. Oval

c. Rectangle

d. Parallelogram

9. A flowchart that are called an initialization block

a. Hexagon

7
b. Oval

c. Rectangle

d. Parallelogram

10. A flowchart that indicates general input and output operations.

a. Hexagon

b. Oval

c. Rectangle

d. Parallelogram

JAVA PROGRAM

An expression produces a result and returns a value. It can be any combination of

variables, literals, and operators.

The following are purposes of expressions:

● to compute values

● to assign values to variables

● to control the flow of execution

Below are common examples of mathematical expressions:

● 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

the result. The different operators used in Java are:

● Arithmetic

● Increment and Decrement

● 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

For example, 15 / 2 is 7, 15 % 2 is 1, and 15.0 / 2 is 7.5. The only operator in this

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,

in the expression 10 % 5, the remainder is 0 because 10 is evenly divisible by 5.

However, the result of 11 % 5 is 1

because 11 divided by 5 is 2 remainder 1. This operator works equally well with

floating-point operands. For example,

12.5 % 1.5 yields the result 0.5.

Note that 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

9
converted to a floating-point number before the operation takes place.

INCREMENT AND DECREMENT

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

causes the variable to be incremented or decremented by 1, and then the new

value is used in the expression in which it appears. However, if used after an

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.

Assigning a value to a variable is an expression because it produces a value. The

assignment operator can also be used to make a series of assignments in a single statement

as shown below:

x = y = z = 7;

In this statement, all three variables end up with the value of

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;

In the expression x = x + 2, x + 2 is calculated first. The result of this calculation is then

assigned to x which makes x

= 7. Using an expression to change a variable's value is a common task in programming.

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

are often more efficient.

Arithmetic assignment operators are best explained by example. For instance, the following

lines are equivalent to each other:

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

Relational operators are used to create comparison expressions that result in a

Boolean value (either true or false). Java provides the following six relational

operators:

== is equal to

< is less than

> is greater than

<= is less than or equal to

>= is greater than or 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

be related (similar). Both operands may be variables, literals, or expressions. Only

== and != may be applied on Boolean data.

LOGICAL OPERATORS

Expressions that result in Boolean values, such as comparison operations, can be

combined to form more complex expressions. This is handled through logical

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

true value only if both Boolean expressions are true.

Consider this example:

boolean extraLife = (score > 75000) & (playerLives < 10);

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

precedence hierarchy to determine the order in which operators are evaluated. In

many cases, this precedence determines the overall value of the expression.

For example, consider the following 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

expression is handled before the one on the right.

Returning to the expression y = 6 + 4 / 2, it shows that division is evaluated before

addition, so the value of y will be 8.

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

innermost parenthetical expression is evaluated first.

The following expression results in a value of 5:

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.

Parentheses also can improve the readability of an expression. If the precedence of

an expression isn't immediately clear to you, adding parentheses to impose the

desired precedence can make the statement easier to understand.

JAVA STATEMENTS

Statements are the tasks you wish your program to do. Statements are your

commands and may comprise keywords, identifiers, and/or mathematical

expressions.

There are some statements that are ready to be used and all we have to do is to

import the proper packages needed in our program.

In Java, a statement is one or more lines of code ending with a semicolon. It


14
generally contains expressions (expressions have a value).

Statements in Java can either be simple statements or compound statements. Simple

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

Decision control structures are Java that allows a programmer to select and

execute specific blocks of code while skipping other sections.

The decision control structure includes types such as If, If- Else, and If-Else-If. We

will be discussing these types later on.

The if statement specifies that a statement (or block of code) will be executed if and

only if a certain Boolean statement is true. It has the format:

if (expression) statement or block; or

if (expression) statement;

where expression represents a relational, equality, or logical expression (conditional

expression). Indent the statements inside the if-block. Such indentation is optional but

it is highly recommended because it emphasizes the inherent structure of the

structured programs.

If there is a need to execute several statements (compound statement), then left and

right {} braces can group these statements.

if (expression)

statement1; statement2;

15
}

Note that placement of the braces is a matter of personal style. Recommendation is

to be consistent with set style and also indent block.

When java encounters an if statement, the first thing it performs is to determine

whether the result of the conditional expression would be true or false. If the result is

true, then it will perform the statement stated in its body.

Should the result of the condition be false, it skips the statement body and proceeds

to the next statement after the if statement.

Making a decision involves choosing between two alternative courses of action based

on some value within a program.

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

another for a FALSE result.

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

connected at a common point.

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

would be evaluated by the computer.

An if-else statement is also capable of performing multiple statements. Always

remember to include the curly braces { } to indicate that a compound or multiple

statements will be performed.

16
The statement in the else-clause of an if-else block can be another if-else structure.

This cascading of structures allows you to make more complex selections.

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

statement. The switch statement enables a computer program to switch between

different outcomes based on a given expression. In contrast to a nested if statement,

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 same outcome.

REPETITION CONTROL STRUCTURES

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.

A do-while loop is similar in structure to a while-loop, except in its upside-down

appearance, as the condition appears after the body. The statements inside a do-

while loop are executed repeatedly as long as the condition is satisfied.

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

do- while loops will terminate at some point.

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

list of data items or initializing an array.

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 sequence of instructions (flowchart is one-dimensional — all blocks are along a

single line). To control program flow, programs then had to jump around instructions.

Structured programming languages provide branching statements to provide

convenient paths while maintaining the “structured” property of the program. Note

that structured programming requires modularity of the program, and that each

module has only one exit point.

Java is a structured programming language and it includes the following three

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

statement which provides the test to control the exit condition.

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

while, do, or for statements.

The return statement is used to exit from the current method. The flow of control

returns to the statement that follows the original method call.

This will be further discussed on the succeeding topics.

POST-TEST:

Direction: Read each statement or question below carefully and choose the correct answer.

1. Is used to provide descriptions of files, methods, data structures, and algorithms.

a. Line comment

b. JavaDoc

c. Block comment

d. None of the Above

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

3. Is used specifically to document classes, attributes, and methods.

a. Line comment

b. JavaDoc

c. Block comment

d. None of the Above

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

5. A fixed set of symbols built into the syntax of the language.

a. Identifiers

b. Literals

c. Data Types

d. Keywords

6. Are items in a program whose values do not change.

a. Identifiers

b. Literals

c. Data Types

d. Keywords

7. Are places in memory that store values.

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

9. A data type that is for numbers without fractional parts.

a. Integer

b. String

c. Boolean

d. Float

10. A data type that numbers with a decimal point.

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.

2. Make sure that your answer is readable.

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

You might also like