Branching ch.3 Flashcards
(35 cards)
myInt == 42
OK
myChar == ‘q’
OK
myDouble == 3.26
NOT OK
myString == “Hello”
NOT OK
=>
not valid operator
!
not valid operator
<>
not valid operator
Can you use use = in an if-else expression?
No, should be ==
How to detect a range?
Use if-else-if-else
How do if-else-if-else structures work?
Each expression only needs to indicate the upper range part; if execution reaches an expression, the lower range part is implicit from the previous expressions being false.
What is the range for if x < 10 else if x < 20 else if x < 30 else
0-9
10-19
20-29
30+
In an if else if else if else structure is the second branch taken if the first if is not false?
the second branch expression is only reached if the first expression is false. So the second branch is taken if userAge < 16 is false (so 16 or greater) AND userAge is < 25, meaning userAge is between 16 - 24 (inclusive).
Which approach uses a logical operator to detect if x is in the range 1 to 99.
0 < x < 100
(0 < x ) && (x < 100)
(0 < x) && (x > 100)
(0 < x) AND (x < 100)
Write expression: x is in the range -4 to +4
(x > -5) && (x < 5)
What happens when you omit the break statement in switch statements? Common error
Omitting the break statement for a case will cause the statements within the next case to be executed. Such “falling through” to the next case can be useful when multiple cases, such as cases 0, 1, and 2, should execute the same statements.
str1 less than str2 returns what?
Negative number
What is the expression for str1 less than str2?
str1.compareTo(str2) < 0
str1 equal to str2 returns what?
0
What is the expression for str1 equal to str2?
str1.compareTo(str2) == 0
str1 greater than str2 returns what?
Positive number
What is the expression for str1 greater than str2?
str1.compareTo(str2) > 0
A programmer can compare strings while ignoring case using
str1.equalsIgnoreCase(str2) and str1.compareToIgnoreCase(str2).
What value does the variable str have at the end of the program segment below? String str = "Hello World!"; if (str.length() > 6) { str = "Changed!"; } // What value does str have here?
“Changed!”
Which of the following is a boolean expression that is true if the variable a has the value of 3?
3 == a
a == 3