Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 3 - 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: 3 stars3 stars3 stars3 stars3 stars / 6
    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


    (Page 3 of 4 )

    The Do ...Loop statement offers another loop that we can use, the Do loop. It's similar to While ...End , but it's more complex, supporting a variety of behaviors. In its most basic form, it's not any different from an infinite While loop, except we're able to omit the expression altogether. Here's a new version of our input-echoing loop, rewritten using Do ...Loop:


    Dim input As   String

    Do

    input = Console.ReadLine()

    Console.WriteLine(input)

    Loop


    If we want to terminate the loop, we have to do so manually by using Exit, as we did earlier with the infinite While loop, since we didn't provide an expression. However, we must specify that we're exiting a Do loop instead of a While loop. Here, we exit if the user types “quit”:


    Dim input As   String

    Do

    input = Console.ReadLine()

    Console.WriteLine(input)

     If input.ToLower() = "quit"   Then

     Exit Do

     End If

    Loop


    The real value in the Do loop, however, lies in its support for multiple behaviors. The loop supports two basic modes of behavior: While and Until . We can either tell our loop to run while a condition is true, or we can tell our loop to do something until a condition is true. Let's take these two behaviors one at a time, starting with the While behavior. Let's create a loop that will echo user input as long as (while) the user does not type “quit”:


    Dim input As   String = ""

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

    input = Console.ReadLine()

    Console.WriteLine(input)

    Loop


    As before, we must assign a value to input. Actually, as you've probably noticed, the above loop bears a strong resemblance to our earlier While loop. Hold that thought for just a moment while we move on to the second mode of behavior, Until. Let's recreate our loop to echo user input until the user types “quit”:


    Dim input As   String = ""

    Do Until input.ToLower() = "quit"

    input = Console.ReadLine()

    Console.WriteLine(input)

    Loop


    Though the above two loops function the exact same way, the expressions used are opposites. In effect, Do Until operates while the expression comes out False . Then, when it comes out True, the loop terminates. Sometimes, this mode of behavior is more straightforward and readable. In fact, it suits our example quite well.

    More Visual Basic.NET Articles
    More By Peyton McCullough


       · 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 6 Hosted by Hostway
    Stay green...Green IT