Presentation is loading. Please wait.

Presentation is loading. Please wait.

Looping While-continue.

Similar presentations


Presentation on theme: "Looping While-continue."— Presentation transcript:

1 Looping While-continue

2 Example Problem: Write a program that calculates the sum of a collection of exam scores. The program should work regardless of class size, hence the program must use the sentinel value to exit. Analyze: Input – exam score Output – sum of exam scores Formula - sentinel loop i. initialize sum to zero ii. get the first score iii. while score is not the sentinel iv. add score to sum v. get next score

3 Continue.. Initialize sum to zero While score is not the sentinel
Incorrect formula for sentinel…WHY ?????? Initialize sum to zero While score is not the sentinel Get score Add score to sum 2 problems: 1. with no initializing input statement, there will be no value for score that is used to judge the loop repetition condition when it is first tested.

4 Continue.. 2. the last statement ,
add score to sum, telling that data value is copied into score and added to the accumulating sum. On the last iteration, the attempt to get another score obtains the sentinel value. This fact will not cause the loop to exit until the loop repetition condition is tested again. Before exit occurs, the sentinel is added to sum. THIS IS NOT RIGHT!!!!! Hence, it is important to set up sentinel- controlled loops using the first method.

5 Continue.. In the first method; there are :
One input to get the loop going-the initialization input A second one is to keep it going – the updating input.

6 Continue.. Sample dialogue when user enter scores 55,33 and 77:
Enter first score or -99 to quit : 55 Enter next score or -99 to quit:33 Enter next score or -99 to quit : 77 Enter next score or -99 to quit:-99 Sum of exam scores is 165

7 pseudocode Begin initialize sum to zero get the first score while score is not the sentinel add score to sum get next score When score equal to sentinel Display sum End

8 Flowchart

9 Example : Shirts are on sale for RM 4 each if more than two are purchased and RM 6 each otherwise. Write a program that able to read in an input number of shirts purchased and display the total cost. Your program must run 5 times before terminate. Upon termination, the total sales in RM must be displayed on screen. Write an appropriate pseudocode for the problem above.

10 solution Analyze: Input – no. shirts
Output- total cost after 5 times running the program Formula – if buy 2, each RM 6 if buy more than 2, each RM 4 Use loop to make the program run 5 times.

11 Solution Begin set c=1 set tcost=0 while c < 6 input number of shirt purchased, n if n>2 tcost=n*4 else tcost=n*6 c=c+1 print “the total cost of shirt is RM :’,tcost end

12 Example Design an algorithm and the corresponding flowchart for adding the test scores as given below: 26, 49, 98, 87, 62, 75

13 Solution – Algorithm-sequential
1. Start 2. Sum = 0 3. Get the first testscore 4. Add first testscore to sum 5. Get the second testscore 6. Add to sum 7. Get the third testscore 8. Add to sum 9. Get the Forth testscore 10. Add to sum 11. Get the fifth testscore 12. Add to sum 13. Get the sixth testscore 14. Add to sum 15. Output the sum 16. Stop

14 Continue..

15 Continue..solve using loop

16 Example An employee earns RM 5 per hour for the first 30 hours and RM 10 per hour for each hour over 30. Write a program that able to compute an employee’s weekly wage. Your program will terminate once user enter a character ‘e’.

17 Algorithm Do: Pseudocode flowchart

18 Example – a revisited version
Keith’s Sheet Music needs a program to implement its music teacher’s discount policy. The program is to prompt the user to enter the purchase total to indicate whether the purchaser is a teacher. This program will keep running until the user enter the sentinel value i.e. -1 to terminate the program. Assumed that the manager of the store need to know the number of teachers and non teacher that buy from the store. Your program must be able to determine this. Music teachers receive a 10% discount on their sheet music purchases unless the purchase total is RM100 or higher. In that case, the discount is 12%. The discount calculation occurs before addition of the 5% sales tax. The sample outputs are as below: two output , 1 for teacher another one is not teacher.

19 Continue.. What you need to do? Analyze the problem - Identify input
Identify output Identify formula 2. Write a pseudo code 3.Draw a flowchart Total purchases RM Teacher’s discount RM Discounted total RM Sales tax RM 5.37 Total RM Total purchases RM Sales tax RM 1.25 Total RM Number of teacher = 1 Number of non teacher =1


Download ppt "Looping While-continue."

Similar presentations


Ads by Google