006 - Control Flow Statements Flashcards
__________ break up the flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code.
Control flow statements
__________ tells your program to execute a certain section of code only if a particular test evaluates to true.
The if-then statement
In an if-then statement, __________are optional, provided that the “then” clause contains only one statement:
the opening and closing braces
__________ provides a secondary path of execution when an “if” clause evaluates to false.
The if-then-else statement
__________ can have a number of possible execution paths.
the switch statement
A switch works with the __________ primitive data types
byte, short, char, and int
A switch works with __________ types, the _________ class, and a few special classes that wrap certain primitive types: __________.
enumerated
String
Character, Byte, Short, and Integer
The body of a switch statement is known as _________.
a switch block
A statement in the switch block can be labeled with _________.
one or more case or default labels
The switch statement evaluates its expression, then executes all statements that __________.
follow the matching case label
Deciding whether to use if-then-else statements or a switch statement is based on __________ and __________.
readability
the expression that the statement is testing
An if-then-else statement can test expressions based on __________ or __________, whereas a switch statement tests expressions based __________, __________, or __________.
ranges of values or conditions
only on a single integer,
enumerated value or
String object.
Each __________ terminates the enclosing switch statement.
break statement
The break statements are necessary because without them, __________.
statements in switch blocks fall through
All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a __________.
break statement is encountered
Technically, a break in __________ is not required because flow falls out of the switch statement.
the final case section
The __________handles all values that are not explicitly handled by one of the case sections.
default section
In Java SE 7 and later, you can use __________ in the switch statement’s expression.
a String object
The __________continually executes a block of statements while a part
while statement
The while statement evaluates __________, which must return __________.
An expression
a boolean value
If __________ in a while control flow evaluates to true, the while statement executes the statement(s) in the while block.
the expression
The while statement continues testing the expression and executing its block until __________.
the expression evaluates to false
The difference between do-while and while is that __________ evaluates its expression at the bottom of the loop instead of the top.
do-while
the statements within the __________ are always executed at least once
do block
The __________ provides a compact way to iterate over a range of values.
for statement
Programmers often refer to a __________ as the “for loop”
For statement
In a for statement, the __________ initializes the loop; it’s executed once, as the loop begins.
initialization expression
When the __________ evaluates to __________, the loop terminates.
termination expression
false
The __________ is invoked after each iteration through the loop
increment expression
In an increment expression, it is perfectly acceptable to _________ or __________ a value.
increment or decrement
The names __________, __________, and __________ are often used to control for loops
i, j and k
declaring _________ within the initialization expression limits their life span and reduces errors
The initialization variable
The for statement also has another form designed for iteration through __________ and __________ This form is sometimes referred to as __________
Collections and arrays
the enhanced for statement
__________ can be used to make your loops more compact and easy to read. To demonstrate
the enhanced for statement
The break statement has two forms: __________ and __________
labeled and unlabeled
You can also use an unlabeled break in a __________, to terminate a __________, __________, or __________ .
switch statement
for, while or do-while loop
An unlabeled break statement terminates the __________ switch, for, while, or do-while statement, but a labeled break terminates __________.
innermost
an outer statement
The __________ skips the current iteration of a for, while , or do-while loop.
continue statement
The __________ skips to the end of the innermost loop’s body and evaluates the boolean expression that controls the loop
unlabeled continue statement
A __________ skips the current iteration of an outer loop marked with the given label.
labeled continue statement
The return statement __________, and control flow returns to __________.
exits from the current method
where the method was invoked
The return statement has two forms: __________, and __________.
one that returns a value
one that doesn’t
To return a value with the return statement, simply put the value (or an expression that calculates the value) __________.
after the return keyword
return ++count;
The most basic control flow statement supported by the Java programming language is the ___ statement.
If-then
The ___ statement allows for any number of possible execution paths.
Switch
The ___ statement is similar to the while statement, but evaluates its expression at the ___ of the loop.
do-while
How do you write an infinite loop using the for statement?
for(;;){
}
How do you write an infinite loop using the while statement?
while(true){
}
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?
Second string
Third string
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.
if (aNumber >= 0) { if (aNumber == 0) { System.out.println( "first string"); } } else { System.out.println( "second string"); } System.out.println("third string");