module 201 object oriented programming lecture 5 looping...
TRANSCRIPT
![Page 1: Module 201 Object Oriented Programming Lecture 5 Looping ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec05.pdf · Object Oriented Programming Lecture 5 – Looping in Code](https://reader036.vdocument.in/reader036/viewer/2022062923/5f0be1037e708231d432aa01/html5/thumbnails/1.jpg)
Module 201 Object Oriented Programming
Lecture 5 – Looping in Code
![Page 2: Module 201 Object Oriented Programming Lecture 5 Looping ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec05.pdf · Object Oriented Programming Lecture 5 – Looping in Code](https://reader036.vdocument.in/reader036/viewer/2022062923/5f0be1037e708231d432aa01/html5/thumbnails/2.jpg)
Conditional branching If statements Single-Line If statements If/Else statements Nested If statements Testing for multiple conditions Comparing a condition to a single value
![Page 3: Module 201 Object Oriented Programming Lecture 5 Looping ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec05.pdf · Object Oriented Programming Lecture 5 – Looping in Code](https://reader036.vdocument.in/reader036/viewer/2022062923/5f0be1037e708231d432aa01/html5/thumbnails/3.jpg)
Sometimes code can determine the number of times to repeat a block of code In some cases we repeat the block as an integer variable iterates between one value and another In other cases we repeat code for each element of a collection of objects
![Page 4: Module 201 Object Oriented Programming Lecture 5 Looping ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec05.pdf · Object Oriented Programming Lecture 5 – Looping in Code](https://reader036.vdocument.in/reader036/viewer/2022062923/5f0be1037e708231d432aa01/html5/thumbnails/4.jpg)
Loops For loops For Each loop Break statement Goto Continue
![Page 5: Module 201 Object Oriented Programming Lecture 5 Looping ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec05.pdf · Object Oriented Programming Lecture 5 – Looping in Code](https://reader036.vdocument.in/reader036/viewer/2022062923/5f0be1037e708231d432aa01/html5/thumbnails/5.jpg)
Repeat a block of code as a variable takes on all the values between 1 and 100? Add separate line for each case?
Awfully tedious….
Like most languages, Visual Basic and C# provide a way to loop through a range of integer values
Can increment or decrement the looping variable By 1 or some other value
![Page 6: Module 201 Object Oriented Programming Lecture 5 Looping ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec05.pdf · Object Oriented Programming Lecture 5 – Looping in Code](https://reader036.vdocument.in/reader036/viewer/2022062923/5f0be1037e708231d432aa01/html5/thumbnails/6.jpg)
The for loop allows you to: Run code repeatedly as integer variable takes on a value
between two endpoints Increment the looping variable by 1, -1 or any other integer value Skip looping variable values by incrementing the looping value
by 2, 3 or some other value Loop backwards, by setting the loop increment to -1 or any
other negative integer Nest loops, providing support for multi-dimensional data
ForLoop.docx ForLoop1.docx ForLoop2.docx ForLoop3.docx
![Page 7: Module 201 Object Oriented Programming Lecture 5 Looping ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec05.pdf · Object Oriented Programming Lecture 5 – Looping in Code](https://reader036.vdocument.in/reader036/viewer/2022062923/5f0be1037e708231d432aa01/html5/thumbnails/7.jpg)
Can use loops to iterate through all the objects returned by a .NET Framework method
DriveInfo.GetDrives returns an array of DriveInfo objects
An array represents multiple objects of the same type
You can index into a list by position, using an index
ListDrives.docx
![Page 8: Module 201 Object Oriented Programming Lecture 5 Looping ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec05.pdf · Object Oriented Programming Lecture 5 – Looping in Code](https://reader036.vdocument.in/reader036/viewer/2022062923/5f0be1037e708231d432aa01/html5/thumbnails/8.jpg)
You can use the For loop to iterate through all the elements of a data structure Requires you to keep track of the end points and manage the index yourself You can also use a For Each loop
Language handles the details for you
ListDrivesForEach.docx
![Page 9: Module 201 Object Oriented Programming Lecture 5 Looping ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec05.pdf · Object Oriented Programming Lecture 5 – Looping in Code](https://reader036.vdocument.in/reader036/viewer/2022062923/5f0be1037e708231d432aa01/html5/thumbnails/9.jpg)
Two important reasons to choose For: If you need a nested loop (with two loops inside each other it
becomes easier to keep track of which loop you’re on) If you need to traverse the data in reverse order (from the end to the
beginning) For Each ONLY moves forward
Why traverse backwards? Good example: Work through a collection and remove each item from
a collection Think about numbering
As you remove items, they renumber Removing from front to back would break the collection
Why not use For Each to remove items? Collections can’t handle dynamic resizing during a loop
For Each uses an index to move on to the next item
![Page 10: Module 201 Object Oriented Programming Lecture 5 Looping ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec05.pdf · Object Oriented Programming Lecture 5 – Looping in Code](https://reader036.vdocument.in/reader036/viewer/2022062923/5f0be1037e708231d432aa01/html5/thumbnails/10.jpg)
Language includes a few more ways to jump about in code
break goto continue
![Page 11: Module 201 Object Oriented Programming Lecture 5 Looping ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec05.pdf · Object Oriented Programming Lecture 5 – Looping in Code](https://reader036.vdocument.in/reader036/viewer/2022062923/5f0be1037e708231d432aa01/html5/thumbnails/11.jpg)
Breaks out of the current block and jumps immediately to the line following the block
Can use the break in switch statements but also in loops
![Page 12: Module 201 Object Oriented Programming Lecture 5 Looping ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec05.pdf · Object Oriented Programming Lecture 5 – Looping in Code](https://reader036.vdocument.in/reader036/viewer/2022062923/5f0be1037e708231d432aa01/html5/thumbnails/12.jpg)
Allows code to jump unconditionally to a label within the current procedure Useful for exiting a deeply nested loop Otherwise, best to avoid Goto Can never use Goto to branch:
Into a For or For Each loop From a Catch statement into a Try statement other than its most
local Try statement Out of a Finally block Into a Catch or Finally block
![Page 13: Module 201 Object Oriented Programming Lecture 5 Looping ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec05.pdf · Object Oriented Programming Lecture 5 – Looping in Code](https://reader036.vdocument.in/reader036/viewer/2022062923/5f0be1037e708231d432aa01/html5/thumbnails/13.jpg)
What if you want to jump back to the top of a loop without executing the rest of the statement in the loop?
Continue statement solves this problem
TestContinue.docx
![Page 14: Module 201 Object Oriented Programming Lecture 5 Looping ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec05.pdf · Object Oriented Programming Lecture 5 – Looping in Code](https://reader036.vdocument.in/reader036/viewer/2022062923/5f0be1037e708231d432aa01/html5/thumbnails/14.jpg)
For loops For Each loop Break statement Goto Continue
![Page 15: Module 201 Object Oriented Programming Lecture 5 Looping ...wiki.hct.ac.uk/_media/computing/fdsc/201oop-lec05.pdf · Object Oriented Programming Lecture 5 – Looping in Code](https://reader036.vdocument.in/reader036/viewer/2022062923/5f0be1037e708231d432aa01/html5/thumbnails/15.jpg)
Introduction to Classes