200 likes | 212 Views
CP1020 - Week 6. Repetition. Aims and Objectives. Understand what loops are and why they are needed Understand the FOR loop used in programming Understand the concept of nesting loops and why they may be necessary Be able to design and code loops in QBasic.
E N D
CP1020 - Week 6 Repetition
Aims and Objectives • Understand what loops are and why they are needed • Understand the FOR loop used in programming • Understand the concept of nesting loops and why they may be necessary • Be able to design and codeloops in QBasic
The Three Main Programming Constructs • So far we have looked at: • Sequence - the ordering of program statements INPUT “the wholesale cost of the article”; fCost LET fRetail = 2 * fCost PRINT “the retail cost of the article is ”; fRetail • Selection - making decisions in our code If...Then...Else... If...Then...Elseif... Select...Case
Repetition • We can now introduce the third construct: • Iteration - looping or repetition • allows one or more statements to be repeated either a fixed number of times or until a particular condition(s) is satisfied
Count Down Loop If Problem THEN Abort Else Ignite rocket Disconnect From Launch Pad
Times Table Problem A program is required to display the times table for a number inputted by the user. • Suppose the number inputted was 6, the program should then generate the 6 times table as follows: 1 x 6 is 6 2 x 6 is 12 3 x 6 is 18 etc... 11 x 6 is 66 12 x 6 is 72
Times Table Initial Design • Based only on what we have done so far:1 Read value in from keyboard2 Display times table for value entered3 End program • This looks OK, but let’s expand step 2...
Times Table Expanded Design • 1 Read value in from keyboard2 Display times table for value entered2.1 Display title2.2 Display 1 * value entered2.3 Display 2 * value entered2.4 Display 3 * value entered2.5 Display 4 * value entered2.6 Display 5 * value entered2.7 Display 6 * value entered
Times table continued 2.8 Display 7 * value entered2.9 Display 8 * value entered2.10 Display 9 * value entered2.11 Display 10 * value entered2.12 Display 11 * value entered2.13 Display 12 * value entered 3 End program
Times Table Code • Code for the long winded solution: DIM iTimes AS INTEGERINPUT iTimes PRINT "The "; iTimes; " Times Table"PRINT "1 * "; iTimes; " = "; 1 * iTimesPRINT "2 * "; iTimes; " = "; 2 * iTimesPRINT "3 * "; iTimes; " = "; 3 * iTimesPRINT "4 * "; iTimes; " = "; 4 * iTimesPRINT "5 * "; iTimes; " = "; 5 * iTimesPRINT "6 * "; iTimes; " = "; 6 * iTimesPRINT "7 * "; iTimes; " = "; 7 * iTimes
Times table problem • PRINT "8 * "; iTimes; " = "; 8 * iTimesPRINT "9 * "; iTimes; " = "; 9 * iTimesPRINT "10 * "; iTimes; " = "; 10 * iTimesPRINT "11 * "; iTimes; " = "; 11 * iTimesPRINT "12 * "; iTimes; " = "; 12 * iTimesEND • Clearly this is very tedious and becomes unworkable as the number of repeat task increases • However I have cunning plan!
Times Table Looping Design • A new design, this time using a For...Next loop1 Read value in from keyboard2 Display title3 Display times table for value entered3.1 For Counter (1 to 12) 3.1.1 Display Counter X value entered End For 4 End program • This type of loop is used to perform a statement (or set of statements) a fixed number of times • This is ideal for our times table problem
For...Next Loop Construct • The For...Next construct should be used when you know how may times the loop should be executed before entering the loop • General structure:FORCount=StartValueTOEndValue[STEPincrement] statements...NEXT[Count]Count can be any of these types: • Integer or Long • Single or Double
Times Table Looping Code • Code for the looping version of the times table: DIM iCount AS INTEGERDIM iTimes AS INTEGERINPUT iTimes CLSPRINT "The "; iTimes; " Times Table"FOR iCount = 1 TO 12PRINT iCount;" * ";iTimes;" = ";iCount * iTimesNEXT iCountEND E.g. 2 * 6 = 12
6 Times Table Example Output The 6 times table 1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36 7 * 6 = 42 8 * 6 = 48 9 * 6 = 54 10 * 6 = 60 11 * 6 = 66 12 * 6 = 72
For...Next Loop Step • We can also specify the step size and count backwards:DIM iCount AS INTEGERFOR iCount = 100 TO 0 STEP -10PRINT "iCount = "; iCountNEXT iCountEND SUB • Unless a STEP is specified the For loopwill increment by 1 each time • You should never change the value ofthe control variable(iCount) inside the For loop
Nesting Loops • Consider the following:A program is required to print out the full 12 times table • A typical screen could be as follows: 1 times table 1 2 3 4 5 6 7 8 9 10 11 12 2 times table 2 4 6 8 10 12 14 16 1 8 20 22 24 3 times table 3 6 9 12 15 18 21 24 27 30 33 36 4 times table 4 8 12 16 20 24 28 32 36 40 44 48 5 times table 5 10 15 20 25 30 35 40 45 50 55 60 6 times table 6 12 18 24 30 36 42 48 54 60 66 72 and so on……...
Design For Full Times Tables Step1 For RowCounter (1 to 12) 1.1 For ColumnCounter (1 to 12) Do1.1.1 Display RowCounter x ColumnCounter End For1.2 Display a new line End For 2 End program
Coding DIM iRowCount AS INTEGER ‘line counterDIM iColumnCount AS INTEGER ‘item on line counterCLSFOR iRowCount = 1 TO 12PRINT iRowCount; "Times table: ";FOR iColumnCount = 1 TO 12PRINT iColumnCount * iRowCount;NEXT iColumnCountPRINTNEXT iRowCountEND
Questions 1 When would you use a For loop? 2 What will the output from the following code be? For iRow = 6 To 4 Step -1 For iCol = -3 To 1 Step 2 Print iRow; " * "; iCol; " = "; iRow * iCol Next iCol Print Next iRow Return to view another lecture