Java Section 4
evaluates to true only if BOTH operands are true (&&)
AND
a type that has just two values: true or false
Boolean
used next to switch statement; diff from boolean expression, can be int char or String; cannot be range of values
Controlling expression
short circuit evaluation of: operand1 && operand2
If the first operand evaluates to true, operand2 is evaluated. If the first operand evaluates to false, the result of the AND operation is always false, so operand2 is not evaluated.
short circuit evaluation: operand1 || operand2
If the first operand evaluates to true, the result of the OR operation is always true, so operand2 is not evaluated. If the first operand evaluates to false, operand2 is evaluated.
Floating-point numbers should be compared for "close enough" rather than exact equality which is written as
Math.abs(x - y) < 0.0001
evaluates to the opposite of the operand (!)
NOT
evaluates to true if ANY operand is true (||)
OR
skips evaluating later operands if the result of the logical operator can already be determined
Short circuit evaluation
a != b means that
a is not equal to b
For integral operand types such as int, & and | represent _________- operators, which perform AND or OR on corresponding individual bits of the operands
bitwise
efers to a quantity that has only two possible values, true or false
boolean
chooses between two or more possible actions - the flow of control splits into two or more possible directions
branching statement
causes execution of that switch statement to end; if left out, execution continues to the next case
break statement
in swtich statement, each case begins with "case" followed by a _______ which is then followed by a colon (:) then one or more statements
case label
in switch statement, braces enclose a set of ______: possible values of the controlling expression
cases
A ________________ has the form condition ? exprWhenTrue : exprWhenFalse. (x == 2) ? 5 : 9 * x evaluates to 5.
conditional expression
? and : together
conditional operator
statements in the ______ case are executed if no other case is matched; not required but recommended
default
The difference threshold indicating that floating-point numbers are equal is often called the
epsilon
Floating-point types should not be compared using the ________ operators, due to the imprecise representation of floating-point numbers
equality
evaluates to true if the left side and the right side are equal; appropriate for determining if two ints, booleans, or chars have the same value; should not be used to compare objects
equality operator ==
An ____________ is a detected runtime error that commonly prints an error message and terminates the program.
exception
If the range has gaps, the range's ends must be (implicitly / explicitly) indicated, using AND
explicitly
Floating-point numbers should not be compared using ==. Ex: Avoid float1 == float2. Reason: ______________
floating point values are approximations; better to specify some value such that if a and b differ by less than that, they are considered equal if (abs(a - b) < epsilon) where a, b, and epsilon are floating point types
the order in which a program performs actions
flow of control
a branch that is taken only if an expression is true
if
executes a group of statements if an expression is true
if statement
When are the statements for a case executed?
if the value of the controlling expression matches the case label of a particular case
this structure has two branches: the first branch is taken if an expression is true, else the other branch is taken
if-else
executes one group of statements when an expression is true, and another group of statements when the expression is false
if-else statement
this structure is when we want to take one of multiple (three or more) branches. each branch's expression is checked in sequence, and as soon as one branch's expression is found to be true, that branch is taken. if no expression is true, execution will reach the else branch, which then executes
ifelse-ifelse
Strings are considered ___________, meaning they cannot be changed
immutable
each string character has a position number called _, which starts at 0 and not 1
index
gets index of first item occurrence in a string, else -1. Item may be char, String variable, or string literal.
indexOf(item)
starts at index indx
indexOf(item, indx)
What is wrong with (s1 == s2)? (where s1 and s2 are strings)
is true if and only if s1 and s2 refer to the same memory location. if s1 and s2 refer to strings with identical sequences of characters, but stored in diff memory locations, (s1 == s2) is false
true if digit: 0-9.
isDigit(c)
true if alphabetic: a-z or A-Z
isLetter(c)
true if whitespace
isWhitespace(c)
finds the last occurrence of the item in a string, else -1.
lastIndexOf(item)
capital letters precede lowercase letters, normal alphabetical ordering applies; ant precedes bug, Cat precedes cat, he precedes hello
lexicographical order
treats operands as being true or false, and evaluates to true or false. they include AND, OR, and NOT
logical operators
when a branch's statements can include any valid statements, including another if-else statement
nested if-else
Omitting the break statement for a case will cause the statements within the ____________ to be executed.
next case
The order in which operators are evaluated in an expression are known as _______________ (), !, * / % + -, < <= > >=, == !=, &&, ||
precedence rules
checks how one operand's value relates to another, like being greater than
relational operator (> < <= >=)
returns a new String in which all occurrences of findChar have been replaced with replaceChar
replace(findChar, replaceChar)
returns a new String in which all occurrences of findStr have been replaced with replaceStr
replace(findStr, replaceStr)
returns a new string that appends s2 to s1
s1.concat(s2)
The method ____________ returns s1's length
s1.length()
The notation ____________ determines the character at index x of a string
someString.charAt(x)
Shorthand for str1 = str1 + str2. str1 must be a String variable, and str2 may be a String variable, a string literal, or a character.
str1 += str2
A programmer compares strings relationally using the notation __________________; returns a neg number if str1 precedes str2, zero if the two strings are equal, pos number if str2 precedes str1; useful for lexicographical order
str1.compareTo(str2)
A programmer can compare two strings using the notation ____________
str1.equals(str2)
A programmer can compare strings while ignoring case using _______________ and _______________
str1.equalsIgnoreCase(str2) str1.compareToIgnoreCase(str2)
The relational and equality operators work for integer, character, and floating-point built-in types. The operators should not be used with _______
strings
returns substring starting at startIndex and ending at endIndex - 1
substring(startIndex, endIndex)
represents multi-branch behavior involving a variable being compared to constant values. the program executes the first case whose constant expression matches the value of the switch expression, executes that case's statements, and then jumps to the end. if no cases match, then the default case statements are executed; should be an integer, char, or string, should not be a Boolean or a floating-point type.
switch statement
an operator that takes three arguments
ternary operator
character operation - Lowercase version
toLowerCase(c)
character operation - Uppercase version
toUpperCase(c)
What do braces in an if-else statement allow programmers to do
use multiple statements / compound statements
they represent a grouping, such as a grouping of statements
{} braces
compares two strings to see if they are equal while ignoring the case of the letters
"Hello".equalsIgnoreCase("hello")
________ to the next case can be useful when multiple cases should execute the same statements
"falling through"