Looping While-continue.

Slides:



Advertisements
Similar presentations
Al-Karma Language School Computer Department Prep. 3.
Advertisements

Introduction to Flowcharting
 Control structures  Algorithm & flowchart  If statements  While statements.
Objectives In this chapter, you will learn about:
1 ICS103 Programming in C Lecture 7: Repetition Structures.
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Designing Algorithms Csci 107 Lecture 3. Designing algorithms Last time –Pseudocode –Algorithm: computing the sum 1+2+…+n –Gauss formula for 1+2+…+n Today.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
An Introduction to Programming with C++ Fifth Edition
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop.
1 Algorithm…. 2 Algorithm: Is a planned set of steps to solve certain problem Ex.: Assume for instance that the gross pay of an employee is to be calculated.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Chapter 12: How Long Can This Go On?
1. Definition and General Structure 2. Small Example 1 3. Simplified Structure 4. Short Additional Examples 5. Full Example 2 6. Common Error The for loop.
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
More While Loop Examples CS303E: Elements of Computers and Programming.
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
Chapter 5 Repetition and Loop Statements J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
PSEUDOCODE C Programming Technique – Firdaus-Harun.com.
Writing Program Code in BASIC Write a program to prompt for and accept values into TWO variables, numx and numy. The program should square the value stored.
EXERCISE IN CLASS CHAPTER 2. PART 1 SEQUENCE SCENARIO 1 Write an algorithm for a C program, that prompts user to enter total number of umbrellas he/she.
Problem : At Kiddy School, the teachers used the A, B,C for grading the students’ works. If the student get an A, then the teacher will print “Excellent”
Control Structures (A) Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Continue with conditional
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
Basic Control Structures
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
1 Program Planning and Design Important stages before actual program is written.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 10.
(C)opyright 2000 Scott/Jones Publishers Introduction to Flowcharting.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
Chapter 7 Problem Solving with Loops
The Hashemite University Computer Engineering Department
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CSE 110: Programming Language I Matin Saad Abdullah UB 404.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
CIS 115 All Exercises Devry University (Devry) FOR MORE CLASSES VISIT CIS 115 All Exercises Devry University.
CIS 115 Slingshot Academy / cis115.com
ALGORITHMS AND FLOWCHARTS
REPETITION CONTROL STRUCTURE
while Repetition Structure
PROGRAM CONTROL STRUCTURE
Chapter 5: Control Structure
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
REPETITION STATEMENTS
Algorithm Algorithm is a step-by-step procedure or formula or set of instruction for solving a problem Its written in English language or natural language.
( Iteration / Repetition / Looping )
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
For Monday Read WebCT quiz 18.
Logical Operators and While Loops
Control Structure Senior Lecturer
Repetition and Loop Statements
Control Structure Senior Lecturer
1) C program development 2) Selection structure
For Wednesday No new reading No quiz.
Let’s all Repeat Together
Logical Operators and While Loops
Continue with conditional
Presentation transcript:

Looping While-continue

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

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.

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.

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.

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

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

Flowchart

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.

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.

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

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

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

Continue..

Continue..solve using loop

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’.

Algorithm Do: Pseudocode flowchart

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.

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 RM122.00 Teacher’s discount RM 14.64 Discounted total RM 107.36 Sales tax RM 5.37 Total RM 112.73 Total purchases RM 24.90 Sales tax RM 1.25 Total RM 26.15 Number of teacher = 1 Number of non teacher =1