C#
  Home arrow C# arrow Page 2 - Branching and Looping in C#, Part 2
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? 
C#

Branching and Looping in C#, Part 2
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 38
    2005-04-19

    Table of Contents:
  • Branching and Looping in C#, Part 2
  • The while Loop
  • The do/while Loop
  • The goto Statement
  • The return statement
  • break and continue statements

  • 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


    Branching and Looping in C#, Part 2 - The while Loop


    (Page 2 of 6 )

    With the for loop we know in advance how many iterations we need, and with the foreach loop we know that it will loop on every and each element (or object) inside the array or inside the collection. Sometimes we need similar behavior; we need to loop through a series of statements until something happens -- maybe the user wants to exit the program, or the user entered invalid data. The while loop construction looks like the following:

    while(boolean expression)
    {
      statement 1;
      statement 2;
      statement 3;
    }

    The while statement executes the block statement as long as its Boolean expression evaluates to true. You can exit the body of the while loop using a jump statement, as we will see soon. Let's take an example. In Part 1 we wrote a simple program that tells if the entered number is even or odd, but this program can only execute one iteration, so let's extend it.

    using System;
    namespace MyCompany
    {
      public class Loops
      {
        static void Main(string[] args)
        {
          int x = 0; 
          while(x != -1)
          { 
            Console.WriteLine("Please type a number, -1 to exist"); 
            x = Convert.ToInt32(Console.ReadLine());
            if(x != -1)
              {
                if(x % 2 == 0) 
                {
                  if(x < 1000) 
                  {
                    Console.WriteLine("this is an EVEN number");
                  }
                  else
                  { 
                    Console.WriteLine("this is a big EVEN number");
                  } 
                } 
                else 
                { 
                  if(x < 1000)
                { 
                  Console.WriteLine("this is an ODD number"); 
                }
                else
                { 
                  Console.WriteLine("this is a big ODD number"); 
                } 
              } 
            } 
          } 
        } 
      } 
    }

     
    Compile and run the program.

    As you can see, the program will not exit until you type -1; also note that the Boolean expression test on the value of x != -1, and in the first statement (before the while loop) we declared and assigned 0 to the variable x. So maybe the while statement never gets executed, because we may put this code in a program that will assign the value of -1 to x before the while statement. We have another way to ensure that the block will be executed at least once before testing the Boolean expression; it's the do/while loop.

    More C# Articles
    More By Michael Youssef


     

    C# ARTICLES

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





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