2Need for RepetitionAn algorithm with get/give, assignment and conditional branch instructions processes one set of data (givens) each time it is executedWhat would we do if we needed to process multiple sets of data?To calculate the grades for 150 students (instead of a grade for one student)To determine the payroll for all employeesWe could execute the algorithm multiple times or once, as in the following example…..

4Loop Block Clearly, we need a better way We need an instruction thatExecutes a block of instructions multiple times, andAfter each execution, tests to see if the block of instructions should be executed againThis instruction is called a loop (repetition) control instruction

5Parts of a Loop There are four parts to a loop SetupThe conditions to be set before we enter the loopTest (included in the loop control instruction)The Boolean test to determine if the loop should be executed again (the same type of test used in a conditional branch instruction)Instruction BlockThe instructions that are to be executed repeatedlyChangeA change to one of the variables in the test so that we can exit the loop

6Two Styles of LoopsUsually we place the test before the instructions that are to be executedHowever, occasionally, we need to execute the instructions before we test; in this case, we place the test after the instructions that are to be executed repeatedly

8Setting up a Loop in an Algorithm (1)SyntaxSet UpLoop When (Test)Instruction 1Instruction 2……….ChangeFinish LoopSemanticAfter the Set Up, the Test is evaluated, if its value is true, instruction1, instruction2,… are executed, and Test is evaluated again. Instruction1, Instruction2 are repeated as long as the Test is true.When the value of Test becomes false, the Loop ends and the instruction following the Loop is executed.

9Setting up a Loop in an Algorithm (2)SyntaxSet UpLoopInstruction 1Instruction 2……….ChangeFinish Loop When (Test)SemanticAfter the Set Up, instruction1, instruction2, … are executed and then Test is evaluated, if its value is true, instruction1, instruction2,… are executed and the Test is evaluated again.When the value of Test becomes false, the Loop ends and the instruction following the Loop is executed.

10Loop InstructionsSince a loop works on a block of instructions, any set of instructions can be repeated, not just assignment and conditional branch instructionsFor example, multiple GetsLoopGet XFinish LoopOr multiple GivesGive Answer

11Algorithm 3.1 (a)Write an algorithm to find the sum of 100 numbers. Place the test at the beginning of the loop.Loop Set UpLet Total = 0Let Count = 0Loop TestContinue until 100 numbers have been read (Count <100)ChangeLet Count = Count + 1InstructionsGet NLet Total = Total + N

20Algorithm 3.3Write an algorithm to find the average of up to 10 numbers entered by the user.Execution of the algorithm stops whenever one of the following conditions occurs10 numbers have been enteredThe user decides to stop entering numbersSetupLet Count = 0, Let Total = 0, Let Again = TrueTestAgain = True AND Count <= 10ChangeLet Count = Count + 1, Get Again

34Flow Charts The Diamond symbol is reused, to test the condition.At the end of the block, the flow will reverse itself back to the top of the loopNote the Test can be at the bottom or the top as stated previously

50In your homework (Lecture 9), you developed algorithms, and tracesreversing the digits in a three digit number and adding that number to 500determining the eldest of 2 peoplecalculating a sales commission For each of these questions, revise the algorithm to:Run the reversing algorithm multiple times until the user enters the value 999Run the eldest algorithm until the user indicates “no” to the question “Do you want to continue?”Run the sales commission algorithm for exactly 10 sales representatives

51Develop an algorithm to assist a clerk in determining some statistics about students. For each student, she enters the name, gender (M or F), age and marital status (M or S). She wants to determine the number of married men, married women, single women and eligible bachelors (single men over 25). Each time she has completed entry of data for a student, the algorithm should give her a chance to indicate whether she has entered the data for all of the students.

52Write an algorithm to determine the closing balance for a tellerWrite an algorithm to determine the closing balance for a teller. The teller enters his opening balance and then a number of transactions. Deposits are entered as positive numbers and withdrawals are entered as negative numbers. He also needs to calculate the number of deposits and the number of withdrawals. The teller indicates that all transactions have been entered by entering a transaction with a value of 0.

53Write an algorithm to calculate and display the total gross earnings, tax payable, medical deduction and net earnings for all employees.The user will input the hours worked and the hourly rate of pay for each employee. The gross earnings for each employee can be calculated from this information.The net earnings are calculated by subtracting the tax payable and medical deductions from the gross earnings. Tax payable is 20% of gross pay for the first $300, 30% for the next $200 and 40% thereafter. The medical deduction is calculated as 1% of gross pay.The user will indicate that all employee information has been entered by entering hours worked as 999.