.Net branching and flow control

681 views

Published on

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

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
681
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \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

    1. 1. .NET - Branchingand Flow Control Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    2. 2. Objectives Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    3. 3. Objectives• Make choices using conditional statements Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    4. 4. Objectives• Make choices using conditional statements• Manage flow control using branching statements Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    5. 5. 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
    6. 6. 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
    7. 7. Agenda Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    8. 8. Agenda• Branching in Code Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    9. 9. Agenda• Branching in Code• Repeating Code Blocks Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    10. 10. Agenda• Branching in Code• Repeating Code Blocks• Unconditional Branching Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    11. 11. Branching in Code Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    12. 12. 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
    13. 13. 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
    14. 14. 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
    15. 15. 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
    16. 16. 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
    17. 17. 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
    18. 18. Conditional Branching Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    19. 19. Conditional Branching• Code can "make decisions" as its executing Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    20. 20. Conditional Branching• Code can "make decisions" as its executing  If statements Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    21. 21. 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
    22. 22. 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
    23. 23. 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
    24. 24. 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
    25. 25. 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
    26. 26. If Statements Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    27. 27. 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
    28. 28. 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
    29. 29. 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
    30. 30. 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
    31. 31. 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
    32. 32. 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
    33. 33. Select Case Statements (VB) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    34. 34. 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
    35. 35. 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
    36. 36. 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
    37. 37. 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
    38. 38. 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
    39. 39. 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
    40. 40. 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
    41. 41. 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
    42. 42. 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
    43. 43. 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
    44. 44. Switch Statements (C#) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    45. 45. 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
    46. 46. 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
    47. 47. 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
    48. 48. 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
    49. 49. 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
    50. 50. 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
    51. 51. Agenda Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    52. 52. Agenda• Branching in Code Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    53. 53. Agenda• Branching in Code• Repeating Code Blocks Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    54. 54. Agenda• Branching in Code• Repeating Code Blocks• Unconditional Branching Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    55. 55. Repeating Code Blocks Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    56. 56. 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
    57. 57. 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
    58. 58. 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
    59. 59. 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
    60. 60. Unbounded Looping Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    61. 61. 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
    62. 62. 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
    63. 63. 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
    64. 64. The While Loop Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    65. 65. The While Loop• Simplest loop: Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    66. 66. 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
    67. 67. 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
    68. 68. 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
    69. 69. 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
    70. 70. 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
    71. 71. The Do Loop (VB) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    72. 72. The Do Loop (VB)• Offers greater flexibility than While loop Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    73. 73. 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
    74. 74. 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
    75. 75. 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
    76. 76. 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
    77. 77. The do…while Loop (C#) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    78. 78. 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
    79. 79. 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
    80. 80. 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
    81. 81. 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
    82. 82. Looping a Specific Number of Times Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    83. 83. 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
    84. 84. 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
    85. 85. 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
    86. 86. The For Loop Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    87. 87. 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
    88. 88. 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
    89. 89. 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
    90. 90. 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
    91. 91. 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
    92. 92. 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
    93. 93. The For Loop Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    94. 94. The For Loop• For loop allows you to: Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    95. 95. 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
    96. 96. 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
    97. 97. 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
    98. 98. 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
    99. 99. 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
    100. 100. Using Loops with the .NET Framework Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    101. 101. 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
    102. 102. 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
    103. 103. 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
    104. 104. 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
    105. 105. The For Each Loop Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    106. 106. 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
    107. 107. 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
    108. 108. 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
    109. 109. 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
    110. 110. Comparing For and For Each Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    111. 111. Comparing For and For Each• Two important reasons to choose For: Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    112. 112. 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
    113. 113. 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
    114. 114. 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
    115. 115. 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
    116. 116. 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
    117. 117. 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
    118. 118. 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
    119. 119. 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
    120. 120. 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
    121. 121. 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
    122. 122. Agenda Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    123. 123. Agenda• Branching in Code Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    124. 124. Agenda• Branching in Code• Repeating Code Blocks Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    125. 125. Agenda• Branching in Code• Repeating Code Blocks• Unconditional Branching Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    126. 126. Unconditional Branching (VB) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    127. 127. 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
    128. 128. 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
    129. 129. 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
    130. 130. 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
    131. 131. Exit Statements (VB) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    132. 132. Exit Statements (VB)• Can explicitly exit an executing loop Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    133. 133. 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
    134. 134. 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
    135. 135. 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
    136. 136. Unconditional Branching (C#) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    137. 137. 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
    138. 138. 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
    139. 139. 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
    140. 140. 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
    141. 141. The break Statement (C#) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    142. 142. 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
    143. 143. 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
    144. 144. The Goto Statement Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    145. 145. 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
    146. 146. 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
    147. 147. 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
    148. 148. 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
    149. 149. 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
    150. 150. 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
    151. 151. 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
    152. 152. 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
    153. 153. Continue Statement Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    154. 154. 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
    155. 155. 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
    156. 156. Learn More! Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    157. 157. 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
    158. 158. 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
    159. 159. 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
    160. 160. 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
    161. 161. 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
    162. 162. 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

    ×