Chapter 5 - Flow Control, Exceptions and Assertions Flashcards
The only legal expression in an if statement is a
boolean expression
what out for boolean assignments
(=) that can be mistaken for a boolean equality test (==)
Curly braces are optional for if blocks
that have only one conditional statement. Watch out for misleading indentations.
switch statements can evaluate only to
enums, byte, short, int and char data types.
The case statement must be literal or a final variable
you cannot have a case that includes a non-final variable, or range of values
the default keyword should be used in a switch statement if
you want to run some code when none of the case values match the conditional code
The default block can be located anywhere in the switch block
if default block does NOT have a break statement, execution will continue to the next line… FALL THROUGH
A basic for statement has 3 parts
declaration and or initialization, boolean evaluation, and the iteration expression.
If a variable is incremented or evaluated within a basic for loop
it must be declared before the loop, or within the for loop declaration
A variable declared (not just initialized) within the basic for loop declaration
cannot be accessed outside the for loop
An enhanced for statement has 2 parts
declaration and expression. Used only to loop through arrays or collections
With an enhanced for
the declaration block is the block variable, whose type is compatible with the elements of the array or collection
You cannot use a number or anything that does not evaluate to a boolean
as a condition for an if statement or looping construct.
The do loop
will enter the body of the loop at least once, even if the test condition is not met