answersLogoWhite

0

What is the use of foreeach loop?

User Avatar

Anonymous

13y ago
Updated: 8/20/2019

Most of programmers start to write programs using arrays, not collection. Arrays are fixed sized collections with indices to "walk" through those collection from beginning to end. Because arrays are fixed sized, the length of an array is known and hence one may walk through every element by for-, while-, or do-while loops in C#.

When you need to apply certain operations to every elements in a collection, and don't care how many elements in that collection, foreach is the natural way to walk through that collection, including arrays in C#.

If I encounter a requirement statements such as "... for every... ", "... for all ...", "for each" (AHA), I prefer foreach statement in code. The traditional loop controls would introduce at least 3 additional programming artifacts (the index, the boundary check, and the increment or decrement).

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

For loop -uses?

You can use a for loop whenever you can use a while loop; it's the same.


Why you use if loop in telecoms?

No such thing as if-loop. if-else statement is not a loop.


How can I use the ffmpeg loop feature to create a continuous video loop?

To create a continuous video loop using the ffmpeg loop feature, you can use the "loop" option in the ffmpeg command followed by the number of times you want the video to loop. For example, you can use the command "ffmpeg -streamloop -1 -i input.mp4 output.mp4" to loop the video indefinitely.


Is it possible to use a for loop in place of a while loop?

Yes. while loop consist of only condition statement to make for loop look as while loop we can use syntax shown below: for(;condition;) eg: for(;i<=n;)


How can you use the "break" statement in Java to prematurely exit a "for" loop?

In Java, you can use the "break" statement within a "for" loop to exit the loop prematurely. When the "break" statement is encountered, the loop will immediately stop executing and the program will continue with the code after the loop.


Why you use for loop inside switch statement?

Because you have to repeat something. (Or you can use while-loop, too.)


Why you need a for loop when you have already while loop or do while loop?

We need a for loop because the while and do-while loops do not make use of a control variable. Although you can implement a counter inside a while or do-while loop, the use of a control variable is not as self-evident as it is in a for loop. Aside from the use of a control variable, a for loop is largely the same as a while loop. However, it is quite different to a do-while loop, which always executes at least one iteration of the loop before evaluating the conditional expression. In a for and while loop, the conditional expression is always evaluated before entering the loop, which may result in the loop not executing at all.


Is there any rule that if I use an If in a loop then i cant use that if in another loop?

If your question makes sense at all, and it is about programming, then the answer is no.


Why you use inner loop and outer loop?

Sometimes you have to use nested loops, in this case one of them is the outer, the other is the inner.


Can break statement be used without any loop?

The Break statement should generally get executed in some loop or switch statement. The below code will give compiler error. void main() { printf("12"); break; printf("14"); }


How do you use even and odd numbers for loop and if condition with variable please no use int?

how to use even and odd number with for loop and if condition plz dont use "int"..


Why your car stereo has a buzzing sound?

you have a ground loop, use a ground loop isolator to get rid of it.