Week 10 Part 2 Flashcards
o Decision structures are meant to perform (or not perform) set(s) of statements ____, repetition structures are only used to perform one set of statement ________
Once, repeatedly
o A while loop is the ______ loop
simplest
o If the Boolean result is false for a while loop before it begins
The body is skipped over and never executed
o What is the best loop if you are not sure how many times the loop will need to repeat?
While loop
o A difference between a while loop and a do-while loop is that in a do while loop the body of the expression will always
Execute at least once
o Unlike other loops, this loop requires a semicolon after the condition
Do-while loop, as no brace comes afterwards
o When are for loops best used?
Counted loops where you know how many repetitions are needed
o The parts in the parenthesis of a for loop are optional, other than the ; . What happens if all parts are omitted?
You will get an infinite loop
o Will this run?
o For( ; index < 42 ; ){
index++; }
o Yes, works like a while loop (don’t do this though)
o Just like if statements the _____ are optional if there’s only 1 statement. This is also a common mistake with loops
Braces
o What happens if we run these?
For (expression) ; {}
while(expression);{}
Infinite loop
o A block of code is typically outlined with
{}
o Loops count as _____ code and variables inside them have a _____-_____ scope
Block, block-level
o It’s best practice to use _____ _____ variables whenever possible in all cases
Local scope
o The _____ and ________ keywords are controversial in java
Break and continue