006 - Control Flow Statements Flashcards

1
Q

__________ break up the flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code.

A

Control flow statements

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

__________ tells your program to execute a certain section of code only if a particular test evaluates to true.

A

The if-then statement

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

In an if-then statement, __________are optional, provided that the “then” clause contains only one statement:

A

the opening and closing braces

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

__________ provides a secondary path of execution when an “if” clause evaluates to false.

A

The if-then-else statement

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

__________ can have a number of possible execution paths.

A

the switch statement

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

A switch works with the __________ primitive data types

A

byte, short, char, and int

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

A switch works with __________ types, the _________ class, and a few special classes that wrap certain primitive types: __________.

A

enumerated
String
Character, Byte, Short, and Integer

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

The body of a switch statement is known as _________.

A

a switch block

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

A statement in the switch block can be labeled with _________.

A

one or more case or default labels

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

The switch statement evaluates its expression, then executes all statements that __________.

A

follow the matching case label

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

Deciding whether to use if-then-else statements or a switch statement is based on __________ and __________.

A

readability

the expression that the statement is testing

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

An if-then-else statement can test expressions based on __________ or __________, whereas a switch statement tests expressions based __________, __________, or __________.

A

ranges of values or conditions

only on a single integer,
enumerated value or
String object.

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

Each __________ terminates the enclosing switch statement.

A

break statement

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

The break statements are necessary because without them, __________.

A

statements in switch blocks fall through

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

All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a __________.

A

break statement is encountered

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

Technically, a break in __________ is not required because flow falls out of the switch statement.

A

the final case section

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

The __________handles all values that are not explicitly handled by one of the case sections.

A

default section

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

In Java SE 7 and later, you can use __________ in the switch statement’s expression.

A

a String object

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

The __________continually executes a block of statements while a part

A

while statement

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

The while statement evaluates __________, which must return __________.

A

An expression

a boolean value

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

If __________ in a while control flow evaluates to true, the while statement executes the statement(s) in the while block.

A

the expression

22
Q

The while statement continues testing the expression and executing its block until __________.

A

the expression evaluates to false

23
Q

The difference between do-while and while is that __________ evaluates its expression at the bottom of the loop instead of the top.

A

do-while

24
Q

the statements within the __________ are always executed at least once

A

do block

25
Q

The __________ provides a compact way to iterate over a range of values.

A

for statement

26
Q

Programmers often refer to a __________ as the “for loop”

A

For statement

27
Q

In a for statement, the __________ initializes the loop; it’s executed once, as the loop begins.

A

initialization expression

28
Q

When the __________ evaluates to __________, the loop terminates.

A

termination expression

false

29
Q

The __________ is invoked after each iteration through the loop

A

increment expression

30
Q

In an increment expression, it is perfectly acceptable to _________ or __________ a value.

A

increment or decrement

31
Q

The names __________, __________, and __________ are often used to control for loops

A

i, j and k

32
Q

declaring _________ within the initialization expression limits their life span and reduces errors

A

The initialization variable

33
Q

The for statement also has another form designed for iteration through __________ and __________ This form is sometimes referred to as __________

A

Collections and arrays

the enhanced for statement

34
Q

__________ can be used to make your loops more compact and easy to read. To demonstrate

A

the enhanced for statement

35
Q

The break statement has two forms: __________ and __________

A

labeled and unlabeled

36
Q

You can also use an unlabeled break in a __________, to terminate a __________, __________, or __________ .

A

switch statement

for, while or do-while loop

37
Q

An unlabeled break statement terminates the __________ switch, for, while, or do-while statement, but a labeled break terminates __________.

A

innermost

an outer statement

38
Q

The __________ skips the current iteration of a for, while , or do-while loop.

A

continue statement

39
Q

The __________ skips to the end of the innermost loop’s body and evaluates the boolean expression that controls the loop

A

unlabeled continue statement

40
Q

A __________ skips the current iteration of an outer loop marked with the given label.

A

labeled continue statement

41
Q

The return statement __________, and control flow returns to __________.

A

exits from the current method

where the method was invoked

42
Q

The return statement has two forms: __________, and __________.

A

one that returns a value

one that doesn’t

43
Q

To return a value with the return statement, simply put the value (or an expression that calculates the value) __________.

A

after the return keyword

return ++count;

44
Q

The most basic control flow statement supported by the Java programming language is the ___ statement.

A

If-then

45
Q

The ___ statement allows for any number of possible execution paths.

A

Switch

46
Q

The ___ statement is similar to the while statement, but evaluates its expression at the ___ of the loop.

A

do-while

47
Q

How do you write an infinite loop using the for statement?

A

for(;;){

}

48
Q

How do you write an infinite loop using the while statement?

A

while(true){

}

49
Q

Consider the following code snippet.

if (aNumber >= 0)
if (aNumber == 0)
System.out.println(“first string”);
else System.out.println(“second string”);
System.out.println(“third string”);

What output do you think the code will produce if aNumber is 3?

A

Second string

Third string

50
Q

Consider the following code snippet.

if (aNumber >= 0)
if (aNumber == 0)
System.out.println(“first string”);
else System.out.println(“second string”);
System.out.println(“third string”);

Using only spaces and line breaks, reformat the code snippet to make the control flow easier to understand.
Use braces, { and }, to further clarify the code.

A
if (aNumber >= 0) {
    if (aNumber == 0) {
          System.out.println(
               "first string");
    }
}
else {
     System.out.println( "second string");
}
System.out.println("third string");