Saturday, 10 October 2015

FOREACH STATEMENT:

foreach statement is a new thing in c#.its quit similar to the for statement but implemented differently.The foreach statement repeats a group of embedded statements for each element in an array or an object collection.
for example:
program: 
/*array using foreach statement*/
using System
class Test
{
    public static void Main()
    {
          int[ ] array={1,2,3,4,5}
          foreach(int a in array)                 //foreach statement where  in is a keyword
          {
               Console.WriteLine(" "+m);
          }
     }
}

foreach statement automatically detect the boundaries of the collection being iterated. After the iteration has been completed for all the elements in the collection, control is transferred to the next statement following the foreach block.
At any point within the foreach block, you can break out of the loop by using the break keyword, or step to the next iteration in the loop by using the continue keyword. 

No comments:

Post a Comment