1 / 13

CP1020 - Week 7

CP1020 - Week 7. Looping constructs. Aims and Objectives. Understand what the while group of loops are and why they are needed Be able to design and code while loops in QBasic. Do While.. Loop Construct.

eblackwell
Download Presentation

CP1020 - Week 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CP1020 - Week 7 Looping constructs

  2. Aims and Objectives • Understand what the while group of loops are and why they are needed • Be able to design and code while loops in QBasic

  3. Do While.. Loop Construct • The DO WHILE…LOOP construct can be used when you don’t know how may times the loop should be executed before entering the loop • The WHILE statement checks to see if the condition is True, if it is then the loop continues to execute • The statements between the DO WHILE and LOOP are executed repeatedly until the condition is FALSE

  4. While loop • General structure:DO WHILEConditionstatements to be repetitively executed….LOOP • The while loop only terminates when the condition is False • If the condition is initially False, the loop may not execute at all !

  5. Lorry will hold up to 1 tonne Programme must tell the operator that this load will exceed the capacity of the store The problem - filling of a grain store Grain store will hold up to 20 tonnes

  6. The problem - filling of a grain store Grain store will hold up to 20 tonnes Grain store will hold up to 20 tonnes Lorry will hold up to 1 tonne Programme must tell the operator that this load will exceed the capacity of the store

  7. The design 1) While store has room, add grain. 1.1 While the grain store can accommodate grain in lorry 1.1.1 Get weight of grain on lorry 1.1.2 add the lorry load to the grain store weight 1.2 Loop 2) Display message not to accept load 3) End

  8. Condition tested INPUT “Enter weight of grain on the lorry”; fLorryWeight fStoreWeight = fStoreWeight + fLorryWeight Statements within loop that will be repetitively executed The programme DIM fLorryWeight AS SINGLE DIM fStoreWeight AS SINGLE PRINT “Grain Store weight system” DO WHILE(fStoreWeight < 20) LOOP PRINT “Do not accept this load as the store will be overfull” END

  9. The DO...LOOP WHILE • The DOWHILE...LOOP will only execute the statements within it, if the condition is initially true. You may wish to have a loop construct that executes it’s statements at least once before exiting the loop • The DO..LOOP WHILE does it’s test at the end of the loop and so will execute it’s statements at least once This last construct is not in the QBasic book

  10. An example problem • A programme is required to determine the wages of employees given the number of hours worked and their rate of pay • The programme should be capable of repeating the calculation for an unspecified number of employees CP1020 principles of programming - Steve Garner and Ian Coulson

  11. The design 1) Calculate the wage of the employees 1.1 do work out employees wage 1.2 Loop till told otherwise • 1.1.1 get the employees hourly rate • 1.1.2 get the employees hours worked • 1.1.3 calculate wage • 1.1.4 display result • 1.1.5 ask the user if they want to quit 2) End

  12. Statements executed within loop INPUT “Hours worked by employee ”; fHours INPUT “Rate of pay for employee”; fRate fWage = fRate * fHours PRINT “employee has earned “; fWage; “ this week” INPUT “Enter q to quit the programme”, sQuit Test condition at end of loop The Programme DIM fWage,fRate, fHours AS SINGLE DIM sQuit AS STRING PRINT “This programme calculates an employees wage” DO LOOPWHILE (sQuit <> “q”) END

  13. Revision questions 1 Why would we use a WHILE loop instead of a FOR loop? 2 Write a section of code to calculate the average of a number of inputs ranging from 1 - 50. Use 66 to quit. 3 Write in three loop constructs a running total for five entered numbers

More Related