Wednesday 1 February 2012

Learn about loops in c#

While Loop:- While loop is use when we have to check a condition and then continues to execute a block of code as long as the condition evaluates to boolean value of true.
Syntax:- while (<boolean expression>) { <statements> }.
using System;
                                class whileloops
                                 {
                                    public static void main()
                                       {
                                          int abc=0;
                                          while(abc<20)
                                            {
                                                console.write("{0}", abc)
                                               abc++;
                                              }


                                                Console.writeline();
                                              }
                                         }
                                   }

For Loop:-  For Loop is use when you know exactly how many time you have to perform the statement with in loop.We have to define the maximum and minimum.

 using Sytem;

 class loops
   {
      Public static void main()
        {
           for(int i=0; i<30; i++)
             {
                  if (i = = 20)
                   break;
                  if (i % 2= =0)
                   continue;
                 console.write("{0}", i);
                 Console.writeline(); 
                                 }
                  }
           }

Foreach Loop:- A for each loop is used to iterate through the items in a list. It operates on arrays or collections such as Array List, which can be found in the System.


using System;

class ForEachLoop
{
    public static void Main()
    {
        string[] names = {"Rahull", "July", "Mahesh", "Sunny"};

        foreach
(string person in names)
        {
            Console.WriteLine("{0} ", person);
        }
    }
}

No comments:

Post a Comment