Presentation is loading. Please wait.

Presentation is loading. Please wait.

CP1020 - Week 7 Looping constructs Aims and Objectives zUnderstand what the while group of loops are and why they are needed zBe able to design and code.

Similar presentations


Presentation on theme: "CP1020 - Week 7 Looping constructs Aims and Objectives zUnderstand what the while group of loops are and why they are needed zBe able to design and code."— Presentation transcript:

1

2 CP1020 - Week 7 Looping constructs

3 Aims and Objectives zUnderstand what the while group of loops are and why they are needed zBe able to design and code while loops in QBasic

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

5 While loop zGeneral structure: DO WHILE Condition statements to be repetitively executed…. LOOP zThe while loop only terminates when the condition is False zIf the condition is initially False, the loop may not execute at all !

6 The problem - filling of a grain store 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 problem - filling of a grain store 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 Grain store will hold up to 20 tonnes

8 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

9 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 Condition tested INPUT “Enter weight of grain on the lorry”; fLorryWeight fStoreWeight = fStoreWeight + fLorryWeight Statements within loop that will be repetitively executed

10 The DO...LOOP WHILE zThe DO WHILE...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 zThe 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

11 CP1020 principles of programming - Steve Garner and Ian Coulson An example problem zA 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

12 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

13 The Programme DIM fWage, fRate, fHours AS SINGLE DIM sQuit AS STRING PRINT “This programme calculates an employees wage” DO LOOPWHILE (sQuit <> “q”) END Test condition at end of loopStatements 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

14 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. 3Write in three loop constructs a running total for five entered numbers


Download ppt "CP1020 - Week 7 Looping constructs Aims and Objectives zUnderstand what the while group of loops are and why they are needed zBe able to design and code."

Similar presentations


Ads by Google