• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
.Net branching and flow control
 

.Net branching and flow control

on

  • 343 views

How to make choices using conditional statements, manage flow control using branching statements and repeat blocks of code using looping statements in .NET.

How to make choices using conditional statements, manage flow control using branching statements and repeat blocks of code using looping statements in .NET.

Statistics

Views

Total Views
343
Views on SlideShare
343
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n
  • DEMO: rest of section\n

.Net branching and flow control .Net branching and flow control Presentation Transcript

  • .NET - Branchingand Flow Control Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Objectives Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Objectives• Make choices using conditional statements Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Objectives• Make choices using conditional statements• Manage flow control using branching statements 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 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Agenda• Branching in Code Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Agenda• Branching in Code• Repeating Code Blocks 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 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 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 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 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 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 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Conditional Branching• Code can "make decisions" as its executing Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Conditional Branching• Code can "make decisions" as its executing  If statements Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Conditional Branching• Code can "make decisions" as its executing  If statements  Single-Line If statements Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Conditional Branching• Code can "make decisions" as its executing  If statements  Single-Line If statements  If/Else statements Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Conditional Branching• Code can "make decisions" as its executing  If statements  Single-Line If statements  If/Else statements  Nested If statements Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Conditional Branching• Code can "make decisions" as its executing  If statements  Single-Line If statements  If/Else statements  Nested If statements  Testing for multiple conditions Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Conditional Branching• Code can "make decisions" as its 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 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 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 isnt true 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 isnt true• Use the If statement different ways 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 isnt true• Use the If statement different ways  Depending on whether you want to execute a single statement or block of statements 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 isnt 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 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 isnt 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 its false Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Select Case Statements (VB) 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: 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 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 youd like to compare 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 youd like to compare  You can use any "value-type" expression in the Select Case statement 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 youd 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 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 youd 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) 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 youd 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 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 youd 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 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 youd 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 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 youd 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#) 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: 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 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 dont include break, execution falls through to next case 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 dont include break, execution falls through to next case  You cannot fall through unless the case includes no code—otherwise, code wont compile 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 dont include break, execution falls through to next case  You cannot fall through unless the case includes no code—otherwise, code wont compile  You can use a string literal as the comparison value 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 dont include break, execution falls through to next case  You cannot fall through unless the case includes no code—otherwise, code wont 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Agenda• Branching in Code Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Agenda• Branching in Code• Repeating Code Blocks 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 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 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 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 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 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 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 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • The While Loop• Simplest 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 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? 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 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 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) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • The Do Loop (VB)• Offers greater flexibility than While loop 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 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 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 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#) 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 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  Its possible youll never enter the loop 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  Its possible youll never enter the loop• Do…While loop checks the condition at the end of the loop 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  Its possible youll never enter the loop• Do…While loop checks the condition at the end of the loop  Guarantees that youll 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 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 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 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 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? 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? 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… 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 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 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • The For Loop• For loop allows you to: 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 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 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 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 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 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 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 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 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 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 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 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 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 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Comparing For and For Each• Two important reasons to choose For: 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 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) 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 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? 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 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 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 As you remove items, they renumber 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 As you remove items, they renumber o Removing from front to back would break the collection 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 As you remove items, they renumber o Removing from front to back would break the collection• Why not use For Each to remove items? 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 As you remove items, they renumber o Removing from front to back would break the collection• Why not use For Each to remove items?  Collections cant handle dynamic resizing during a loop Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Agenda Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Agenda• Branching in Code Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Agenda• Branching in Code• Repeating Code Blocks 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) 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 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 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 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) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • Exit Statements (VB)• Can explicitly exit an executing loop 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 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 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#) 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 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 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 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#) 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 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 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 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 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 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: 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 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 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 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 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? 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! 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 @ 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 @ 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: 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 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 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