L03: Selection Flashcards
What are the 3 conditional statements?
If, if-else, switch
What happens if we try to compare chars for a conditional?
Converts to int based on Unicode character and compares.
What can we use to compare 2 strings?
.equals() method
How does the compareTo() method work?
Compares the combined ascii value of one string to another string. If string 1 is greater than string 2, returns +. If String 1 is less than string 2, returns -. If they are equal, returns 0;
When should & and | be used
To avoid lazy evaluation.
How are else statements evaluated regardless of indentation?
The else is matched with the first unmatched if unless {} are used.
The switch case has two reserved words, what are they and explain what they do?
break: when a case is matched, executes expression and exits switch.
default: The default case if there are no matching cases.
Can a switch be replaced by if-else?
Yes
What happens if you don’t but a break in the switch?
The case that matches will execute the statement and flow down until a break is reached.
What’s another way of writing a conditional? (shorthand)
condition? statement1: statement2 .
If condition true -> statement1
else -> statement2
NOTE that the value can be stored in a variable or even be used in a print.