Presentation is loading. Please wait.

Presentation is loading. Please wait.

CP1020 - Week 6 Repetition. Aims and Objectives zUnderstand what loops are and why they are needed zUnderstand the FOR loop used in programming zUnderstand.

Similar presentations


Presentation on theme: "CP1020 - Week 6 Repetition. Aims and Objectives zUnderstand what loops are and why they are needed zUnderstand the FOR loop used in programming zUnderstand."— Presentation transcript:

1 CP1020 - Week 6 Repetition

2 Aims and Objectives zUnderstand what loops are and why they are needed zUnderstand the FOR loop used in programming zUnderstand the concept of nesting loops and why they may be necessary zBe able to design and code loops in QBasic

3 The Three Main Programming Constructs zSo far we have looked at: zSequence - 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 zSelection - making decisions in our code If...Then...Else... If...Then...Elseif... Select...Case

4 Repetition zWe can now introduce the third construct: zIteration - looping or repetition yallows one or more statements to be repeated either a fixed number of times or until a particular condition(s) is satisfied

5 Count Down Loop If Problem THEN Abort Else Ignite rocket Disconnect From Launch Pad

6 Times Table Problem A program is required to display the times table for a number inputted by the user. zSuppose 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

7 Times Table Initial Design zBased only on what we have done so far: 1 Read value in from keyboard 2 Display times table for value entered 3 End program zThis looks OK, but let’s expand step 2...

8 Times Table Expanded Design z1Read value in from keyboard 2Display times table for value entered 2.1Display title 2.2Display 1 * value entered 2.3Display 2 * value entered 2.4Display 3 * value entered 2.5Display 4 * value entered 2.6Display 5 * value entered 2.7Display 6 * value entered

9 Times table continued 2.8Display 7 * value entered 2.9Display 8 * value entered 2.10Display 9 * value entered 2.11Display 10 * value entered 2.12Display 11 * value entered 2.13Display 12 * value entered 3 End program

10 Times Table Code zCode for the long winded solution: DIM iTimes AS INTEGER INPUT iTimes PRINT "The "; iTimes; " Times Table" PRINT "1 * "; iTimes; " = "; 1 * iTimes PRINT "2 * "; iTimes; " = "; 2 * iTimes PRINT "3 * "; iTimes; " = "; 3 * iTimes PRINT "4 * "; iTimes; " = "; 4 * iTimes PRINT "5 * "; iTimes; " = "; 5 * iTimes PRINT "6 * "; iTimes; " = "; 6 * iTimes PRINT "7 * "; iTimes; " = "; 7 * iTimes

11 Times table problem zPRINT "8 * "; iTimes; " = "; 8 * iTimes PRINT "9 * "; iTimes; " = "; 9 * iTimes PRINT "10 * "; iTimes; " = "; 10 * iTimes PRINT "11 * "; iTimes; " = "; 11 * iTimes PRINT "12 * "; iTimes; " = "; 12 * iTimes END zClearly this is very tedious and becomes unworkable as the number of repeat task increases zHowever I have cunning plan!

12 Times Table Looping Design zA new design, this time using a For...Next loop 1Read value in from keyboard 2Display title 3Display times table for value entered 3.1 For Counter (1 to 12) 3.1.1Display Counter X value entered End For 4End program zThis type of loop is used to perform a statement (or set of statements) a fixed number of times zThis is ideal for our times table problem

13 For...Next Loop Construct zThe For...Next construct should be used when you know how may times the loop should be executed before entering the loop zGeneral structure: FOR Count = StartValue TO EndValue [STEP increment] statements... NEXT [Count] Count can be any of these types: yInteger or Long ySingle or Double

14 Times Table Looping Code zCode for the looping version of the times table: DIM iCount AS INTEGER DIM iTimes AS INTEGER INPUT iTimes CLS PRINT "The "; iTimes; " Times Table" FOR iCount = 1 TO 12 PRINT iCount;" * ";iTimes;" = ";iCount * iTimes NEXT iCount END E.g. 2 * 6 = 12

15 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

16 For...Next Loop Step zWe can also specify the step size and count backwards: DIM iCount AS INTEGER FOR iCount = 100 TO 0 STEP -10 PRINT "iCount = "; iCount NEXT iCount END SUB zUnless a STEP is specified the For loop will increment by 1 each time zYou should never change the value of the control variable (iCount) inside the For loop

17 Nesting Loops zConsider the following: A program is required to print out the full 12 times table z 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……...

18 Design For Full Times Tables Step 1 For RowCounter (1 to 12) 1.1 For ColumnCounter (1 to 12) Do 1.1.1 Display RowCounter x ColumnCounter End For 1.2 Display a new line End For 2End program

19 Coding DIM iRowCount AS INTEGER ‘line counter DIM iColumnCount AS INTEGER ‘item on line counter CLS FOR iRowCount = 1 TO 12 PRINT iRowCount; "Times table: "; FOR iColumnCount = 1 TO 12 PRINT iColumnCount * iRowCount; NEXT iColumnCount PRINT NEXT iRowCount END

20 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


Download ppt "CP1020 - Week 6 Repetition. Aims and Objectives zUnderstand what loops are and why they are needed zUnderstand the FOR loop used in programming zUnderstand."

Similar presentations


Ads by Google