Week 9 Part 2 Flashcards
o “else” vs. “else if” at end of an if statement, what is the difference in behaviour?
o “else” at the end of an if statement means that one of the expressions will run, no matter what. With “else if” an if condition must be true before anything executes.
o T/F – Else can be used by itself.
False
o T/F – You can nest if in if-else selection structures.
True
o Java coding conventions – an if statement must be indented _ tab or _ spaces
1 tab, 4 spaces
- Problems with if that are really hard to debug
Forgetting to use {braces}.
- What logic error can occur if you forget to add braces to your if statement and there are 2 subsequent statements?
o 1 statement is run rather than 2, and the 2nd one is interpreted as a regular line of code
o This can be hard to catch as the 2nd line will likely also be indented
- What does a semicolon do in an if structure?
o It ends the if structure, causing subsequent lines to always execute
o hasNextInt() and hasNextDouble() return_______ data types and are used for
Boolean, User input data validation
o Compares the value provided in the (brackets) with each case until it finds a match or exits the structure
- Java switch structure (case structure)
o T/F – All case structures must have a default option
False.
o When a case is matched in a switch structure, the statements under it are executed until a ____ statement
Break
o If the break is omitted from a switch case, what happens?
The program begins executing case statements that are underneath the entered case
o Each case in a case structure must have a _______ _____, no variables. Constants are okay.
Literal value
o Per common practice, default is usually placed at the ___ of the case structure
End
o T/F – You can nest a switch inside of an if, and vice versa
True