Control Flow Flashcards

1
Q

What goes inside an if statement’s parentheses?

A

A boolean expression

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

What goes inside an else statement’s parentheses?

A

Nothing, there are no parentheses.

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

Where is an else valid?

A

Only after an if statement.

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

How deep can ifs and elses be nested?

A

As deep as you make them, but nesting deep makes the code more difficult to read.

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

Does a switch require a default case?

A

No, it’s optional. The default keyword marks a catch-all case. If no other case matches, the default case executes.

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

Can we “fall through” (execute all cases after a match) a switch?

A

Switch cases “fall through” if there’s no break keyword. Once a case matches, it runs the rest of the code in the switch until it hits a break.

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

Can we switch on a String value?

A

Yes

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

Can we switch on a LocalDate?

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

Identify the three parts of a for loop inside its parentheses. Are all parts required?

A

1) The first initializes variables or state.
2) The second is a boolean expression. If it evaluates to true, the for’s code block executes, just like a while statement.
3) The third clause runs after each loop. It can be used to update variables or state.

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

Is this valid?:

for (int x = 5, y = 10; x < y && x > -y; x+=2, y++) { }

Does it terminate?

A

It does not cause an exception but it does terminate.

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

What can a while loop compute that a for loop can’t?

A

A for loop cannot recreate.

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

The do/while loop seems unnecessary. Is it?

A

A do/while must be used when you want the loop to be evaluated once, and if the condition is met, then evaluate it again.

The main difference with a while loop is that it is evaluated at least once. Of course you can evaluate the loop and then begin witha a while loop.

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

What is an enhanced for loop? What can it operate on?

A

It is mainly used to traverse a collection of elements including array

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

if statements are useful when:

A

We are evaluating an expression, like x < 5, instead of checking a specific value.
Conditions are complex.
A range of values triggers a single code block.

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

switch statements are useful when:

A

We have a relatively small, fixed set of values to evaluate.

Each value’s branching is unique

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

When should you use a for loop?

A

A for loop is a good choice when we know the number of repetitions required.