Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7COSC 181 – Foundations of Computer ProgrammingClass 10Announcement1st TestFri. Feb 20thComprehensive through the end of MondayClosed Note/Closed BookRead and Interpret Code“trace” codeWrite Code SnippetsSmall programs or parts of programsSyntax matters!Multiple-Choicewhile Repetition StatementRepetition statementAction repeated while some condition remains truePseudocodeWhile there are more items on my shopping list Purchase next item and cross it off my list •while loop repeats until condition becomes falseExample•int product = 3;while ( product <= 100 ) product = 3 * product;•What will product equal when the loop stops executing?What does this example do?Example•int product = 3;while ( product <= 100 ) product = 3 - product;Try ItUse a single while statement to write the code for our last homework.Read in 20 charactersTell me how many a’s, e’s, i’s, o’s, and u’s there are.Common Error - #1Initialize each counter and total, either in its declaration or in an assignment statement. Totals are normally initialized to 0. Counters are normally initialized to 0 or 1, depending on how they are used (we will show examples of when to use 0 and when to use 1).Common Error - # 2Using a loop’s counter-control variable in a calculation after the loop often causes a common logic error called an off-by-one-error. In a counter-controlled loop that counts up by one each time through the loop, the loop terminates when the counter’s value is one higher than its last legitimate value (i.e., 11 in the case of counting from 1 to
View Full Document