Module 201 Object Oriented Programming Lecture 5 – Looping in Code
Module 201 Object Oriented Programming
Lecture 5 – Looping in Code
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
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
Loops For loops For Each loop Break statement Goto Continue
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
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
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
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
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
Language includes a few more ways to jump about in code
break goto continue
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
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
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
For loops For Each loop Break statement Goto Continue
Introduction to Classes