Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 2 - 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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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 / 5
    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 - While...End continued


    (Page 2 of 4 )

    The code in the previous section will accept user input, echo the input, and then stop as soon as it encounters the Exit statement. Typically, though, we'll want to attach some sort of condition to our Exit statement (otherwise, why use a loop?). For example, we can choose to exit the loop only if the user types “quit”:


    Dim input As   String

    While True

    input = Console.ReadLine()

    Console.WriteLine(input)

     If input.ToLower() = "quit"   Then

     Exit While

     End If

    End While

    If the user types “quit,” then we exit. We convert the input to lower case in order to get around case sensitivity. That way, the user can type “QUIT,” “QuIt,” etc., and the effect will be the same. In any case, the above code is bad. We evaluate an expression inside of the loop and then exit depending on the result. So, in effect, we're ignoring the basic functionality provided for us by the loop. Let's rewrite our loop to make it better (and shorter in code length) by incorporating the condition into the loop itself:


    Dim input As   String = ""

    While input.ToLower() <> "quit"

    input = Console.ReadLine()

    Console.WriteLine(input)

    End While


    As you can see, we've replaced True with a more complex expression. In fact, this expression is the opposite of the expression we previously used in our If statement. Instead of checking to see if the user has typed “quit,” we check to see if the user has not typed quit. If the user hasn't, then we run the loop. The combination of less-than and greater-than symbols (<>) in the expression is actually the “not equal to” operator. Although it wouldn't be as elegant (and, indeed, I'm just pointing it out here to introduce a new operator without going too much off course), we could have instead added the Not operator to negate the earlier expression:


    While Not input.ToLower() = "quit"


    Also, be sure to note that we assign a value to input before we run the loop. This is because we can't run the next line's comparison using an unassigned variable. The code will compile, but the compiler will generate a warning, and an exception will be thrown at runtime.

    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

    - 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...
    - Creating and Drawing a Game Map in VB.NET (F...
    - Working with Classes and Properties for Game...
    - Working with Loops, Arrays, and Collections ...
    - Learning Loops in VB.NET for Game Development
    - Learning VB.NET: Working with Variables, Con...
    - The Basics of VB.NET Through Text Game Devel...
    - Learning VB.NET Through Text Game Development
    - Types of Operators in Visual Basic
    - Operators
    - Understanding Custom Events using Visual Bas...

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT