Iteration Statements

Discussion in 'C#' started by Programmer.Nick, Dec 20, 2006.

  1. Programmer.Nick

    Programmer.Nick New Member

    Joined:
    Dec 19, 2006
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0

    Introduction



    Iteration Statements are often known as looping, and they run many times while specific condition is true. I would try to explain some of them here. The lesson is for a begginer, and I guess that everyone need to know them.

    While



    The While loop function used and while the condition is true, the loop statement executed.
    the genral form of while loop:

    Code:
     
    while (Boolean exception)
             statement or block of statements
    
    and here is an example of a program, that time the value of i between 0 to 20:

    Code:
    using System;
    
    class Program
    {
        static void Main(string[] args)
        {
            int i;
            i=0;
            while (i < 20)
            {
                Console.WriteLine(i);
                i++;
            }
        }
    }
    so, know that whe knows what is the While loop statement is lets go for anothe statment of loop.

    Do...While



    The do while is exactly as the While loop, and its run while the condition is true, and when the condition is true the compiler goes to the do block statement, and he runs it.

    The genral Form OF the Do....while:

    Code:
    do
            statement or a block of statement
    while(booleans expression);
    
    and here is an example
    Code:
    using System;
    
    class Program
    {
        static void Main(string[] args)
        {
            int i;
            i=0;
            do
            {
                Console.WriteLine(i);
                i++;
            }while(i<20);
        }
    }
    
    In this line of codes , the compiler do the same thing he done at the while Program.
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Can't we write the code like this?

    Code:
    using System;
    
    class Program
    {
        static void Main(string[] args)
        {
            int i;
            i=0;
            while (i++ < 20)
                Console.WriteLine(i);
        }
    }
    
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    There will be difference in the output
     
  4. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    How and why will it be different?
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Your code will print 1-20 and the sample would 0-19.
    You should use i++ in WriteLine function instead.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice