Java Reference
In-Depth Information
a. value1 <= value2
b. (value1 + 5) >= value2
c. value1 < value2 / 2
d. value2 != value1
e. !(value1 == value2)
f. (value1 < value2) || done
g. (value1 > value2) || done
h. (value1 < value2) && !done
i. done || !done
j. ((value1 > value2) || done) && (!done || (value2 >
value1))
SR 5.5
What is a truth table?
SR 5.6
Assuming done is a boolean variable and value is an int variable, cre-
ate a truth table for the expression:
(value > 0 ) || !done
SR 5.7
Assuming c1 and c2 are boolean variables, create a truth table for the
expression:
(c1 && !c2) || (!c1 && c2)
5.2 The if Statement
We've used a basic if statement in earlier examples in this chapter. Let's now
explore it in detail.
An if statement consists of the reserved word if followed by
a boolean expression, followed by a statement. The condition is
enclosed in parentheses and must evaluate to true or false. If the
condition is true, the statement is executed and processing continues
with the next statement. If the condition is false, the statement is
skipped and processing continues immediately with the next state-
ment. Figure 5.6 shows this processing.
Consider the following example of an if statement:
KEY CONCEPT
Proper indentation is important for
human readability; it shows the rela-
tionship between one statement and
another.
if (total > amount)
total = total + (amount + 1);
In this example, if the value in total is greater than the value in amount , the
assignment statement is executed; otherwise the assignment statement is skipped.
Note that the assignment statement in this example is indented under the
header line of the if statement. This communicates that the assignment statement
 
Search WWH ::




Custom Search