Java Control Structures I
There are three ways to process a computer program.
In a sequence (executing statements in order)
By making a selection or choice (branching) based on certain conditions (conditional statements)
Repeating statements a certain number of times (looping)
Conditional
statements can be created using the word if. A condition is met if it
evaluates to true and then certain statements are executed. If it
evaluates to false other statements are executed.
Relational Operators
In
Java, a condition is represented by a logical (Boolean) expression (an
expression that has a value of true or false when evaluated). These
expressions are created using relational operators (a binary operator
consisting of two operands) and can be used to make comparisons. Some
relational operators in Java include:
== equal to
!= not equal to
< less than
<= less than or equal to
> greater than
>= greater than or equal to
Relational Operators and Primitive Data Types
Relational
operators can be used with integral and floating-point primitive data
types. For char values, whether an expression evaluates to true or
false depends on the collating sequence of the Unicode character set.
When Java evaluates a logical expression, it returns the Boolean value
true if the expression evaluates to true and false otherwise.
Comparing Strings
In
Java, strings are compared character by character, starting with the
first character and using the collating sequence. The
character-by-character comparison continues until one of three
conditions is met: a mismatch is found, the last characters have been
compared and are equal, or one string is exhausted.
If
two strings of unequal length are compared, and they are equal until
the last character of the shorter string, then the shorter string is
evaluated as less than the larger string.
The
method compareTo in the class String can be used to compare objects of
the class. This expression returns an integer value as follows:
Str1.compareTo(str2) = integer value less than 0 if string str1 less than string str2
Str1.compareTo(str2) = 0 if string str1 equal to string str2
Str1.compareTo(str2) = integer value greater than 0 if string str1 greater than string str2
The method equals can be used to compare two strings and determine if they are equal.
Logical (Boolean) Operators and Logical Expressions
Logical (Boolean) operators enable you to combine logical expressions. The three logical (Boolean) operators in Java are:
! not (unary)
&& and (binary)
|| or (binary)
Logical operators take only logical values as operands and yield only logical values as results.
When
you use the not operator, !true is false and !false is true. Putting !
in front of a logical expression reverses the value of that logical
expression.
&&
and || are used to evaluate a combination of expressions. An
expression using && only returns true if ALL expressions are
true. || returns true as long as one expression in the combination is
true.
Order of Precedence
Logical expressions have the following order of precedence when evaluating expressions:
Operators Precedence
! + - (unary operators) first
* / % second
+ - third
< <= >= > fourth
== != fifth
&& sixth
|| seventh
= (assignment operator) last
Relational and logical operators are evaluated from left to right; their associativity is from left to right.
Short-Circuit Evaluation
Logical
expressions in Java are evaluated using a high efficient algorithm
known as 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 is known, the evaluation
stops.
Teaching Tip
The
operators & and | can be used in place of && and ||,
respectively. There is no short-circuit evaluation with these
operators.
boolean Data Type and Logical (Boolean) Expressions
You can manipulate logical (Boolean) expressions using the boolean data type and Java reserved words boolean, true and false.
One-Way Selection
In Java, one-way selections are incorporated using the if statement. The syntax of one-way selection is:
if (expression)
statement
The
expression, which is a logical expression, is referred to as the
decision maker because it decides whether to execute the statement
(called the action statement).
Two-Way Selection
To choose between two alternatives Java provides the if...else statement. Two-way selection uses the following syntax:
if (expression)
statement1
else
statement2
If
the value of the expression is true then statement1 executes; otherwise
statement2 executes. The else statement must follow an if statement;
it does not exist on its own in Java.
Compound (Block of) Statements
The
if and if...else structures control only one statement at a time. To
permit more complex statements, Java provides a structure called a
compound statement or block of statements. Block statements are
enclosed in curly braces {} and consist of a sequence of statements to
be executed depending on the evaluation of the if and if…else
expressions.
Multiple Selections: Nested if
You
can include multiple selection paths in a program by using an if...else
structure, if the action statement itself is an if or if...else
statement. When one control statement is located within another, it is
said to be nested. Nested statements help solve problems that require
the implementation of more than two alternatives.
In
a nested if statement, Java associates an else with the most recent
incomplete if—that is, the most recent if that has not been paired with
an else.
Comparing if...else Statements with a Series of if Statements
A
series of if statements can be used in place of if…else statements to
complete a task. However, the program may execute more slowly in this
case because there are more evaluations to be made.
Conditional Operator (? :)
Certain
if...else statements can be written more concisely by using Java’s
conditional operator. The conditional operator, written as ?:, is a
ternary operator, which means that it takes three arguments. The syntax
for using the conditional operator is:
expression1 ? expression2 : expression3
If
expression1 evaluated to true, the result of the conditional expression
is expression2. Otherwise, the result of the conditional expression is
expression3.
switch Structures
The
second selection structure in Java does not require the evaluation of a
logical expression. It is called the switch Structure and gives the
computer the power to choose from many alternatives. The switch
statement has the following syntax:
switch(expression)
{
case value1: statements1
break;
case value2: statements2
break;
…
case valuen; statementsn
break;
default: statements
}
The
expression (sometimes called the selector) is evaluated first and the
value of the expression is then used to perform the actions specified in
the statements that follow the reserved word case.
The
expression is usually an identifier and the value is always an
integral. The value of the expression determines which statement is
selected for execution, therefore a particular case values must be
unique.
One or more statements may follow a case symbol, and curly braces are unnecessary to group them.
The break statement may or may not appear after each statement.
A switch statement executes according to the following rules:
When
the value of the expression is matched against a case value (also
called a label), the statements execute until either a break statement
is found or the end of the switch structure is reached.
If
the value of the expression does not match any of the case values, the
statements following the default label execute. If the switch structure
has no default label, and if the value of the expression does not match
any of the case values, the entire switch statement is skipped.
A break statement causes an immediate exit from the switch structure
Walang komento:
Mag-post ng isang Komento