answersLogoWhite

0

difference between for and for each loop..?

The for loop executes a statement or a block of statements repeatedly until a specified expression evaluates to false. there is need to specify the loop bounds( minimum or maximum).

int j = 0;

for (int i = 1; i <= 5; i++)

{

j = j + i ;

}

The foreach statement repeats a group of embedded statements for each element in an array or an object collection.you do not need to specify the loop bounds minimum or maximum.

int j = 0;

int[] tempArr = new int[] { 0, 1, 2, 3, 5, 8, 13 };

foreach (int i in tempArr )

{

j = j + i ;

}

User Avatar

Wiki User

14y ago

What else can I help you with?

Continue Learning about Engineering

What is the difference between for-loop container and foreach-loop container in ssis?

foreach loop executes a predetermined number of times eg. list of items, rows in a table, etc. a for loop executes until a certain condition is met


What is the syntax of foreach in c?

There is no 'foreach' in C


Is it mandatory to use array in foreach loop C-sharp can foreach be used without array?

Foreach is for an IEnumerable, not just an array (unless you are using C# 1.1 or earlier). A collection (List, Dictionary, etc) can also generate a sequence from foreach-statement. In fact, if you have a class that is NOT an array, but it implements all the required methods of IEnumerable, it may be applied as:public class Storage: IEnumerable {...}Storage myStorage = new Storage();...Foreach (Thing stuff in myStorage){...}


Why can we use foreach loop for arrays although arrays do not implement Iterable?

In PHP, the foreach loop can be used with arrays because it is specifically designed to work with array data structures, even though arrays do not implement the Iterable interface. The foreach construct is able to iterate over arrays directly, utilizing the internal array handling mechanisms of the language. This allows for a more straightforward syntax for looping through elements without the need for additional constructs. Thus, foreach provides a convenient and efficient way to process arrays in PHP.


What are the advantages of using a foreach loop over a for or while loop for processing list elements?

foreach can simply replace for loop. for ex in perl, if you need to copy display an array, it will take only foreach $var(@arr). then print $var. no need of incrementing the index of array like for loop.

Related Questions

What is the difference between for-loop container and foreach-loop container in ssis?

foreach loop executes a predetermined number of times eg. list of items, rows in a table, etc. a for loop executes until a certain condition is met


What is the syntax of foreach in c?

There is no 'foreach' in C


What is the importance of foreach loop in php?

The foreach construct simply gives an easy way to iterate over arrays. Foreach works only on arrays (and objects).


Is it mandatory to use array in foreach loop C-sharp can foreach be used without array?

Foreach is for an IEnumerable, not just an array (unless you are using C# 1.1 or earlier). A collection (List, Dictionary, etc) can also generate a sequence from foreach-statement. In fact, if you have a class that is NOT an array, but it implements all the required methods of IEnumerable, it may be applied as:public class Storage: IEnumerable {...}Storage myStorage = new Storage();...Foreach (Thing stuff in myStorage){...}


How do you writte foreach loop in net?

Assuming your data store implements the "IEnumerable" interface (or the IEnumerable generic interface) foreach (type arg in datastore) { ... } So, for example, if you have a List, it would be List list = new List() ... foreach (string val in list) { Console.Out.WriteLine(val); }


What purpose is foreach function in php?

foreach is a simple loop mostly used to read an array line by line; $arr = array("one", "two" , "three"); foreach($arr as $c) { echo $c; } it'll output : onetwothree also try looking up the while loop


Which loop is used in php?

while, do...while, for, foreach are used


What is the PHP foreach construct used for?

The PHP foreach construct is used to iterate over arrays. This is done in the field of mathematics. It will issue errors when one tries to use it as a variable instead of arrays and objects.


Difference between jdk1.4 and jdk1.5?

1. AutoBoxing 2. Introducing enum 3. Add Generic 4. Add new foreach loop 5. Add variable number of Arguments to the function 6. Add some new class like StringBuilder 7. Add System.out.printf(); many more


Why can we use foreach loop for arrays although arrays do not implement Iterable?

In PHP, the foreach loop can be used with arrays because it is specifically designed to work with array data structures, even though arrays do not implement the Iterable interface. The foreach construct is able to iterate over arrays directly, utilizing the internal array handling mechanisms of the language. This allows for a more straightforward syntax for looping through elements without the need for additional constructs. Thus, foreach provides a convenient and efficient way to process arrays in PHP.


What is foreach statement give example?

The foreach statement is a control structure in programming that allows you to iterate over elements in a collection, such as arrays or lists, without needing to manage an index variable. It simplifies the syntax for looping through elements. For example, in PHP: $fruits = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]; foreach ($fruits as $fruit) { echo $fruit . &quot; &quot;; } This code outputs each fruit in the array.


What are the advantages of using a foreach loop over a for or while loop for processing list elements?

foreach can simply replace for loop. for ex in perl, if you need to copy display an array, it will take only foreach $var(@arr). then print $var. no need of incrementing the index of array like for loop.