Ch 5 - Control Statements Flashcards

1
Q

What do control statements do?

A

Causes flow of execution to advance and branch

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

What are the two main types of control statements and what do they do?

A

Selection: choose different paths of execution based on outcome of an expression

Iteration: repeat statements

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

What are the two types of selection statements?

A

if and switch

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

What is an if statement?

A

Binary program flow control
based on a condition

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

what is the switch statement and why would you use it?

A

A multi-path flow control.

The value is compared to each of the values in the case statements and executed when it matches.

Switch (i) {
case 0: {
do something
break; }

case 1: {
do something else
break;
}

default: neither
}

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

Can a switch statement evaluate any boolean expression or just equals?

A

Just equals.

Need to us if for other evaluations (>, <, etc)

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

Is a switch statement usually more efficient than nested if statements?

A

yes

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

Name the 3 iteration statements?

A

for, while, do-while

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

Why use a do-while?

A

Want it to run at least once

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

Explain for loops

A

for (initialization; condition; iteration)

initialize the loop control variable
condition is evaluated
Iteration looped

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

Can a for loop have multiple statements in the initialization and iteration portions?

for (initialization; condition; iteration)

A

Yes

for (int a=1, b=2; a < b; a++, b–)

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

Can a for loop have empty statements ?

for (initialization; condition; iteration)

A

yes, can do following which is infinite loop that runs forever.

for ( ; ; )

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

What is a for-each style loop?

A

cycles through a collection of objects, like an array.

int nums[] = {1, 2, 3, 4, 5, 6,7, 8};
int sum = 0;

for ( int x: nums) sum += x;

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

Why would you use a for-each style loop?

A

To compare to a collection of items.

Useful for search. Can find where in a unsorted set a value is present.

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

True or false: Multidimensional arrays consist of arrays of arrays.

A

True

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

What does this do?

A

Multiple cases for 1 action.
All would return english

17
Q

For a switch statement, can you use a string?

A

Yes, after Java 7

case “case1”:

18
Q
A