Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , ,

Sponsored Links



How to use Looping in C#?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 09-02-2010
DANIEL 602's Avatar
Member
 
Join Date: Jul 2006
Posts: 212
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
Reply With Quote
  #2  
Old 09-02-2010
Warner's Avatar
Member
 
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.
Reply With Quote
  #3  
Old 09-02-2010
Member
 
Join Date: Nov 2008
Posts: 1,193
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();
        }
    }
}
Reply With Quote
  #4  
Old 09-02-2010
opaper's Avatar
Member
 
Join Date: May 2008
Posts: 2,362
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();
        }
    }
}
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #5  
Old 09-02-2010
Zecho's Avatar
Member
 
Join Date: May 2008
Posts: 2,267
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);
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to use Looping in C#?"
Thread Thread Starter Forum Replies Last Post
Output not looping in C++ Guntoor Software Development 6 3 Weeks Ago 10:19 PM
Stuck with bar looping in vista 32 bit Ajmil Windows Software 5 26-06-2011 11:01 PM
Continuous Looping In iPad Video Depo Portable Devices 5 25-12-2010 10:19 PM
Mozilla goes looping the same Page Zadora Technology & Internet 9 26-03-2009 11:09 AM
VBA: Looping Through Tasks StGhurka Microsoft Project 2 15-01-2009 03:25 AM


All times are GMT +5.5. The time now is 03:52 AM.