Module 201 Object Oriented Programming Lecture 5 Looping ... · Object Oriented Programming Lecture...

15
Module 201 Object Oriented Programming Lecture 5 – Looping in Code

Transcript of Module 201 Object Oriented Programming Lecture 5 Looping ... · Object Oriented Programming Lecture...

Page 1: Module 201 Object Oriented Programming Lecture 5 Looping ... · Object Oriented Programming Lecture 5 – Looping in Code ... by 2, 3 or some other value Loop backwards, by setting

Module 201 Object Oriented Programming

Lecture 5 – Looping in Code

Page 2: Module 201 Object Oriented Programming Lecture 5 Looping ... · Object Oriented Programming Lecture 5 – Looping in Code ... by 2, 3 or some other value Loop backwards, by setting

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 ... · Object Oriented Programming Lecture 5 – Looping in Code ... by 2, 3 or some other value Loop backwards, by setting

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 ... · Object Oriented Programming Lecture 5 – Looping in Code ... by 2, 3 or some other value Loop backwards, by setting

Loops For loops For Each loop Break statement Goto Continue

Page 5: Module 201 Object Oriented Programming Lecture 5 Looping ... · Object Oriented Programming Lecture 5 – Looping in Code ... by 2, 3 or some other value Loop backwards, by setting

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 ... · Object Oriented Programming Lecture 5 – Looping in Code ... by 2, 3 or some other value Loop backwards, by setting

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 ... · Object Oriented Programming Lecture 5 – Looping in Code ... by 2, 3 or some other value Loop backwards, by setting

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 ... · Object Oriented Programming Lecture 5 – Looping in Code ... by 2, 3 or some other value Loop backwards, by setting

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 ... · Object Oriented Programming Lecture 5 – Looping in Code ... by 2, 3 or some other value Loop backwards, by setting

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 ... · Object Oriented Programming Lecture 5 – Looping in Code ... by 2, 3 or some other value Loop backwards, by setting

Language includes a few more ways to jump about in code

break goto continue

Page 11: Module 201 Object Oriented Programming Lecture 5 Looping ... · Object Oriented Programming Lecture 5 – Looping in Code ... by 2, 3 or some other value Loop backwards, by setting

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 ... · Object Oriented Programming Lecture 5 – Looping in Code ... by 2, 3 or some other value Loop backwards, by setting

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 ... · Object Oriented Programming Lecture 5 – Looping in Code ... by 2, 3 or some other value Loop backwards, by setting

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 ... · Object Oriented Programming Lecture 5 – Looping in Code ... by 2, 3 or some other value Loop backwards, by setting

For loops For Each loop Break statement Goto Continue

Page 15: Module 201 Object Oriented Programming Lecture 5 Looping ... · Object Oriented Programming Lecture 5 – Looping in Code ... by 2, 3 or some other value Loop backwards, by setting

Introduction to Classes