L03: Selection Flashcards

1
Q

What are the 3 conditional statements?

A

If, if-else, switch

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What happens if we try to compare chars for a conditional?

A

Converts to int based on Unicode character and compares.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What can we use to compare 2 strings?

A

.equals() method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does the compareTo() method work?

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When should & and | be used

A

To avoid lazy evaluation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How are else statements evaluated regardless of indentation?

A

The else is matched with the first unmatched if unless {} are used.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

The switch case has two reserved words, what are they and explain what they do?

A

break: when a case is matched, executes expression and exits switch.

default: The default case if there are no matching cases.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Can a switch be replaced by if-else?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What happens if you don’t but a break in the switch?

A

The case that matches will execute the statement and flow down until a break is reached.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What’s another way of writing a conditional? (shorthand)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly