.NET - Branching and Flow Control
Learn More @ http://www.learnnowonline.com Copyright Š by Application Developers Training Company
Objectives • Make choices using conditional statements • Manage flow control using branching statements • Repeat blocks of code using looping statements • Break out of loops when necessary
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Agenda • Branching in Code • Repeating Code Blocks • Unconditional Branching
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Branching in Code • Any programming language must provide means of branching in code • Procedure may need to execute one statement if a condition is true Optionally, execute a second statement if the condition is false
• .NET provides several different techniques for making decisions as code executes Conditional branching covered here Unconditional branching covered as necessary Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Conditional Branching • Code can "make decisions" as it's executing
If statements Single-Line If statements If/Else statements Nested If statements Testing for multiple conditions Comparing a condition to a single value
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
If Statements • Allows you to execute a block of code if a condition is true Skips the code if the condition isn't true
• Use the If statement different ways Depending on whether you want to execute a single statement or block of statements
• Can also execute one block if a statement is true And another if it's false Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Select Case Statements (VB) • Select Case is simple to use, but notice specific details: Select Case statement can match on literal, property, or calculated expression Include a Case statement for each value you'd like to compare You can use any "value-type" expression in the Select Case statement o
If you can use = to compare it, it will work
Case statements can include comparisons other than equality (the default comparison) Case statements can include comma-delimited lists of values Visual Basic evaluates Case statements from top to bottom o
As soon as it finds one that matches, it looks at no others
If no other cases match, Visual Basic executes code in the else block Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Switch Statements (C#) • Switch statement is simple to use, but note these issues: The break statement causes C# to jump out of the switch to the line immediately following the switch If you don't include break, execution falls through to next case You cannot fall through unless the case includes no code—otherwise, code won't compile You can use a string literal as the comparison value If no other cases match, C# executes the code in the default case Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Agenda • Branching in Code • Repeating Code Blocks • Unconditional Branching
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Repeating Code Blocks • Several different code constructs that allow you to run code repeatedly • Can execute code while or until a condition is/becomes true • Can execute code a fixed number of times • Can execute a block of code once for each element of a collection
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Unbounded Looping • Several different options available for repeating a block of code while a condition remains true While loop Do loop
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
The While Loop • Simplest loop: Executes a block indefinitely, while a condition remains true
• What if condition never changes? Code must exit loop on demand
• More likely to exit when the condition changes state Examples show both techniques
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
The Do Loop (VB) • Offers greater flexibility than While loop Perhaps too much flexibility
• Allows you to check the condition at the top or bottom of the loop Allows you to loop while or until the condition remains true or changes
• Placing check for the condition at the bottom of the loop guarantees that the code runs at least once Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
The do…while Loop (C#) • While loop checks the condition at the top of the loop It's possible you'll never enter the loop
• Do…While loop checks the condition at the end of the loop Guarantees that you'll run the code within the loop at least once
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Looping a Specific Number of Times • Sometimes code can determine the number of times to repeat a block of code • In some cases, repeat block as an integer variable iterates between one value and another • In other cases, repeat code for each element of a collection of objects
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
The For Loop • Repeat a block of code as a variable takes on all the values between 1 and 100? • Add a 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
The For Loop • 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 negative integer Nest loops, providing support for multi-dimensional data
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Using Loops with the .NET Framework • Can use loops to iterate through all the objects returned by a .NET Framework method DriveInfo.GetDrives returns an array of DriveInfo objects
• Array represents multiple objects of the same type Can index into list by position, using an index
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
The For Each Loop • Can use 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 • Can also use a for each loop Language handles details for you
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Comparing For and For Each • Two important reasons to choose For: If you need a nested loop If you need to traverse the data in reverse order (from the end back to the beginning) o
For Each only moves forward
• Why traverse backwards? Good example: Remove each item from a collection Think about numbering o o
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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Agenda • Branching in Code • Repeating Code Blocks • Unconditional Branching
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Unconditional Branching (VB) • Language includes a few more ways to jump about in code Exit For/Exit While/Exit Do Goto Continue
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Exit Statements (VB) • Can explicitly exit an executing loop Exit For, Exit While, Exit Do
• If you have nested loops, you can exit from any of the loops, at any level Loops must be of different types for this to work
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Unconditional Branching (C#) • Language includes a few more ways to jump about in code break goto continue
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
The break Statement (C#) • Breaks out of the current block, and jumps immediately to the line following the block Can use break in switch statements, but also in loops
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
The Goto Statement • Allows code to jump unconditionally to a label within the current procedure • Useful for exiting a deeply nested loop • Otherwise, it’s 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Continue Statement • What if you want to jump back to the top of a loop without executing the rest of the statements in the loop? Continue statement solves this problem
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
Learn More! • This is an excerpt from a larger course. Visit www.learnnowonline.com for the full details! • Learn more about .NET on SlideShare: • Getting Started with .NET • .NET Variables and Data Types • Using the .NET Framework
Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company