Week 9 Part 1 Flashcards
o Permits the program to make a decision, based on data and logic, resulting in different sequence structures to be executed.
- Selection Structure, also called Decision Structure
o A selection structure starts with a ______ _________, and then takes of two paths
Boolean expression
o Most programming languages have a selection structure, either __, __-____-____, and a ____ _________ (called switch in Java)
If, if-then-else, and a case structure
o Obtains a user input, checks the input against one condition
- Single-alternative selection structure
- Would be able to determine if a user number is less, than 10, but nothing more.
Obtains a user input, checks the input against two conditions
- Dual-alternative selection structure
- Would be able to determine if a user number is more or less than 10
Obtains user inputs, checks the input against 1 decision, and then 2 nested decisions
- Nested Selection Structure
o Using these combination operators, we can make more complicated decisions, such as determining if a number is within or outside of a range of values.
AND and OR
- Testing Boundary Cases
o We test inputs that are just at, around, or beyond the minimum and maximum limits
o This must be remembered when using the NOT, AND & OR logical operators together
Order of operations
o What is the bug in this code?
if age <= 12 OR age >= 65 AND rating = “G” then
output “Discount applies”
Endif
Add brackets around the or to separate it from the AND
o Can be used to flip logic which can simplify a selection structure
- NOT operator (negative logic)
o When can a NOT operator be useful?
When you need to only take action if the condition is false
it’s easier for another programmer to understand
What’s better than this
If number = 42
else
output “not 42”
endif
if number != 42
output “not 42”
endif
o Case structure is also known as ______ in Java
Switch
o Case structure matches a value against ______ ______
single values