You are on page 1of 40

CONTROL STRUCTURE

(SELECTION)
JAVA NC III

M.A. ESPIRITU
Control Structures
A computer can process a program in one
of three ways:
• In sequence
• By making a selection or a choice, which
is also called a branch
• By repetition, executing a statement over
and over using a structure called a loop
Flow of execution
Control Structures

The two most common control


structures are selection and
repetition.
Relational Operators
An expression that has a value of either true or
false is called a logical (boolean) expression. The
values true and false are called logical (boolean)
values.

In Java,
A condition is represented by a logical (boolean)
expression; conditions are either true or false.
Relational Operators
Relational Operators and Primitive Data Types
Relational Operators and Primitive Data Types
Relational Operators and Primitive Data Types
Logical (Boolean) Operators and Logical
Expressions

• Logical operators take only logical values as


operands and yield only logical values as
results. The operator ! is unary, so it has only
one operand. The operators && and || are
binary.
Logical (Boolean) Operators and Logical
Expressions
Logical (Boolean) Operators and Logical
Expressions
Logical (Boolean) Operators and Logical
Expressions
Logical (Boolean) Operators and Logical
Expressions
Logical (Boolean) Operators and Logical
Expressions
Order of Precedence
To work with complex logical expressions, there
must be some priority scheme for
determining which operators to evaluate first.
Because an expression might contain
arithmetic, relational, and logical operators, as in the
expression 5 + 3 <= 9 && 2 > 3,
an order of precedence for the Java operators must be
established.
Order of Precedence
Order of Precedence
Order of Precedence
SELECTION: IF AND
IF...ELSE
One-Way Selection
Note the elements of this syntax. It begins with the
reserved word if, followed by a logical expression
contained within parentheses, followed by a statement.

The logical expression is also called a condition; it decides


whether to execute the statement that follows it.

If logical expression is true, the statement executes. If it is


false, the statement does not execute and the computer
goes on to the next statement in the program.
One-Way Selection
One-Way Selection
Two-Way Selection
In a two-way selection, if the
value of the logical expression is
true, then
statement1 executes. If the value
of the logical expression is false,
then statement2
executes.
Two-Way Selection
Two-Way Selection
Compound (Block of) Statements
Multiple Selections: Nested if
Comparing if...else Statements with a Series
of if Statements
Comparing if...else Statements with a Series
of if Statements
Short-Circuit Evaluation

In the first statement, the two operands of the


operator || are the expressions (x > y) and (x == 5).
This expression evaluates to true if either the
operand (x > y) is true or the operand (x == 5) is
true.
Short-Circuit Evaluation
With short-circuit evaluation, the computer evaluates the
logical expression from left to right. As soon as the value of the
entire logical expression can be determined, the evaluation stops.
For example, in the first statement, if the operand (x > y)
evaluates to true, then the entire expression evaluates to true
because true || true is true and true || false is true. Therefore, the
value of the operand (x == 5) has no bearing on the final outcome.
Short-Circuit Evaluation
Short-Circuit Evaluation
SWITCH STRUCTURES
SWITCH STRUCTURE
Java’s switch structure gives the computer the power to choose
from many alternatives.

The switch structure does not require the evaluation of a logical


expression
SWITCH STRUCTURE
SWITCH STRUCTURE
SWITCH STRUCTURE
THANK YOU
NEXT LESSON: RULES OF NETIQUETTE

You might also like