130 likes | 144 Views
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.
E N D
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 • 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
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 !
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
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
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
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
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
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
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
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
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