Results 1 to 5 of 5

Thread: How to use Looping in C#?

  1. #1
    Join Date
    Jul 2006
    Posts
    273

    How to use Looping in C#?

    Hi friends,
    I am new to this forum, so please ignore my mistakes. I am new to C# programming language. But I am not new to programming language since I have done C programming language. I have done looping in the C language. I know the concepts of looping, but I know that the type that used in C must be different then C#. So please anyone can tell me the correct way to use the looping in C#..?? Please help me soon..!!
    (\__/)
    (='.'=) This is Bunny. Copy and paste bunny into your
    (")_(") signatureto help him gain world domination

  2. #2
    Join Date
    Mar 2008
    Posts
    349

    Re: How to use Looping in C#?

    Because of the looping in any language, you get an ability to repeat a block of code for n number of times. There are four different ways of loops in C#, they are :
    • The while loop
    • The do loop
    • The for loop
    • The foreach loop

    You can use each loop according to your need.

  3. #3
    Join Date
    Nov 2008
    Posts
    1,192

    Re: How to use Looping in C#?

    I think that the While Loop is the most simple loop in C#. The while loop simply executes a block of code as long as the condition you give it is true. Check the below example, so that you can know it very clearly :
    Code:
    using System;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                int number = 0;
    
                while(number < 10)
                {
                    Console.WriteLine(number);
                    number = number + 1;
                }
    
                Console.ReadLine();
            }
        }
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: How to use Looping in C#?

    If you know how many times you want to run the code before executing, then you can use the For Loop. You can also use it if you have a variable containing the amount. See the example of For Loop below :
    Code:
    using System;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                int number = 10;
    
                for(int i = 0; i < number; i++)
                    Console.WriteLine(i);
    
                Console.ReadLine();
            }
        }
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: How to use Looping in C#?

    If you have worked on While loop in other language, the Do Loop is almost similar to that loop. The do loop evaluates the condition after the loop has executed, due to which it is assured that code block is always executed at least once.Just glance an example below :
    Code:
    do
    {
        Console.WriteLine(number);
        number = number + 1;
    } while(number < 10);

Similar Threads

  1. Stuck with bar looping in vista 32 bit
    By Ajmil in forum Windows Software
    Replies: 5
    Last Post: 26-06-2011, 10:01 PM
  2. remote keypress looping endlessly Harmony One
    By Erakna in forum Hardware Peripherals
    Replies: 4
    Last Post: 04-04-2011, 11:57 PM
  3. Continuous Looping In iPad Video
    By Depo in forum Portable Devices
    Replies: 5
    Last Post: 25-12-2010, 10:19 PM
  4. Mozilla goes looping the same Page
    By Zadora in forum Technology & Internet
    Replies: 9
    Last Post: 26-03-2009, 10:09 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,515,820.57455 seconds with 16 queries