Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 4 - Learning Loops in VB.NET for Game Developm...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

Learning Loops in VB.NET for Game Development
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2008-06-18

    Table of Contents:
  • Learning Loops in VB.NET for Game Development
  • While...End continued
  • Do...Loop
  • Do...Loop continued

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Learning Loops in VB.NET for Game Development - Do...Loop continued


    (Page 4 of 4 )

    Earlier, I pointed out the Do  While example and the While example looked and performed very similarly. Actually, though, there's one important difference: the Do loop can test the expression after the loop's instructions are run. Consider, for example, the following While loop:


    While False

    Console.WriteLine("Hello.")

    End While


    Since the expression is False, the call to WriteLine within the loop will never actually be executed. Execution will essentially skip right over the loop. Sometimes, this is appropriate behavior since we may never want a loop to run if the expression comes out False in the beginning. However, there are situations where we want the loop to run at least once, and the While loop is not fitted to these situations. This can all be fixed if, instead of checking the expression before each iteration, we check the expression after each iteration. To enable this behavior in a Do loop, we simply need to specify the expression after Loop rather than after Do. The following loop, for example, will execute exactly once, even though the expression is False:


    Do

    Console.WriteLine("Hello.")

    Loop While   False


    We can make use of this behavior with our previous example, too. Before, we had to assign a blank string to input because its value was checked before the user was able to actually input anything. We can eliminate the need for this assignment, however, by checking the expression after the loop iterates. At this point, the user will have entered something and the variable will have been assigned a value:


    ' No assignment:

    Dim input As   String

    Do

    input = Console.ReadLine()

    Console.WriteLine(input)

    Loop While input.ToLower() <> "quit"


    This behavior also works with Until :


    Dim input As   String

    Do

    input = Console.ReadLine()

    Console.WriteLine(input)

    Loop Until input.ToLower() = "quit"


    This behavior models the behavior of the generic do loop in other languages, so it's the behavior with which most programmers will be familiar. As you've seen, though, the Visual Basic version of the loop is more flexible. However, a Do loop should not be used in situations where a simple While loop will suffice.

    Next up are the For loop and the For Each loop.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Hello,This is a continuation of my series on learning Visual Basic through the...
     

    VISUAL BASIC.NET ARTICLES

    - User-defined Functions using Visual Basic Ap...
    - Understanding Object Binding in VBA
    - Mastering the Message Box
    - Testing a Windows Forms Application
    - Using Visual Basic.NET Features to Code a Wi...
    - Correcting Code in a Windows Forms Applicati...
    - Write Readable Code and Comments for Windows...
    - How to Code and Test a Windows Forms Applica...
    - Adding Features to a Windows Forms Applicati...
    - How to Design a Windows Forms Application
    - LINQ to XML Programming Using Visual Basic.N...
    - Understanding Delegates using Visual Basic.N...
    - Create a Sudoku Puzzle Generator using VB.NET
    - Entity Creation and Messaging in a VB.NET Te...
    - Movement and Player Statistics in a VB.NET T...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek