Lecture 11, CS 302-6, September 28
i. While
ii. For
iii. Do…while
i. Doesn’t check condition before first iteration
ii. Post-test instead of pre-test
do
{
}
while(condition);
i. You know you need to get an input at least once
ii. You want to keep going while you haven’t met the necessary condition
iii. So, you want to get the input within the loop – but you don’t know beforehand what the value will be
i. Indefinite
ii. Aren’t required to do it
iii. For generic use
1. One example: bank account. Keep going until it’s empty, but you don’t know when that will happen
i. Indefinite
ii. Required to do it at least once
iii. Examples – input validation
i. Definite
ii. Aren’t required to do it
iii. Examples – counting, iterating
i. A value for input that indicates that we are done accepting values. The border between our data set and everything else.
ii. Real life example – period at the end of a sentence?
iii. Computer example – in a menu, quit
i. while(value!=some number)
ii. while(!done)
iii. while(in.hasNextDouble()), while(in.hasNextInt()), etc.
i.
Standard
deviation, variance, etc
i. For each character in a string if it is the character we are looking for, add one to a counter
i. Determining if a word contains a letter, substring, capital, etc
i. Keep track of largest, smallest as you go
i. One pointer to previous, one to current
i. Examples in nature (?): Rabbit reproduction, nautilus shells, sunflower flowers, golden ratio, etc
1. F0=0,F1=1, fn=fn-1+fn-2 for n>1