Friday 26 October 2012

What is the difference between for and foreach loop?

foreach loop or statement is same as for loop or statement with a slight difference.In foreach loop , loop irterate through array and collections. Unlike for loop, foreach loop does not have test condition means it does not check any condition.
Example:

foreach Loop:

foreach( data_type data_variable in collection_name)
{
// your code
}

for loop:

for(data_type variable ;condition;update )
{
// your code
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.