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 benested. 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. Theconditional 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.
Call me and you will get the knowledge and skills you need instantly.
For inquiries contact me on my mobile number or email me at inquiry@eglobiotraining.com. View my multiply site http://erwinglobio.multiply.com
Prof. Erwin M. Globio, MSIT
Senior IT Lecturer
Far Eastern University
Email Address: inquiry@eglobiotraining.com
Skype: erwinglobio
Call Now:
SMART: 09393741359
SUN: 09323956678
No comments:
Post a Comment