Question
When should i use a foreach loop rather than a for?

Answers

By using foreach, you are letting the compiler decide on the optimization rather than worrying about it yourself. For example - are pointers or indices better? Should I cache the termination condition or not? Should I rotate the loop or not? The answers to these questions are not easy, and can vary from machine to machine. Like register assignment, let the compiler do the optimization.
for (int i = 0; i < foo.length; i++)or:for (int i = 0; i < foo.length; ++i)or:for (T* p = &foo[0]; p < &foo[length]; p++)or:T* pend = &foo[length];for (T* p = &foo[0]; p < pend; ++p)or:T* pend = &foo[length];T* p = &foo[0];if (p < pend){    do    {        ...    } while (++p < pend);}and, of course, should I use size_t or int?for (size_t i = 0; i < foo.length; i++)Let the compiler pick!foreach (v; foo)    ...
Note that we don't even need to know what the type T needs to be, thus avoiding bugs when T changes. I don't even have to know if foo is an array, or an associative array, or a struct, or a collection class. This will also avoid the common fencepost bug:
for (int i = 0; i <= foo.length; i++)
And it also avoids the need to manually create a temporary if foo is a function call.
The only reason to use a for loop is if your loop does not fit in the conventional form, like if you want to change the termination condition on the fly.   Your Comment




More Software Questions..
What is the inputsplit in map reduce software?

What is software configuration management?

What Is Java Api For Xml-based Rpc (jax-rpc)?

How can you implement fine-grained auditing?

What is IBM’s simple explanation for Big Data’s four critical features?

What is static synchronized method in JDBC API? Give an example?

What does the NULLIF function do?

What happens if a start method is not invoked and the run method is directly invoked?

Should we override finalize method

what is the difference between mysql_fetch_array and mysql_fetch_object?

How will XML affect my document links?

Why to use Style Sheets?

What are Filters in MVC?

Can you explain Application layer in OSI model?

How to define new testplan attributes?

What are the minimum system requirements to run Photoshop? Is it possible to run Photoshop over linux?

Which oracle package is used to manage the oracle lock management services?

What is Latch Up? Explain Latch Up with cross section of a CMOS Inverter. How do you avoid Latch Up?

What is marker interface?

What types of partitioning are there for BW?



Search
Can you Answer!!
  • Q Which organelle is present in bacteria or prokaryotic cell ?
  • Q Which of the following code is correct ? a. FileWriter fileWriter = new FileWriter("../file.txt"); File file = new File(fileWriter ); BufferedWriter bufferedOutputWriter = new BufferedWriter(fileWriter); b. BufferedWriter bufferedOutputWriter = new BufferedWriter("../file.txt"); File file = new File(bufferedOutputWriter ); FileWriter fileWriter = new FileWriter(file); c. File file = new File("../file.txt"); FileWriter fileWriter = new FileWriter(file); BufferedWriter bufferedOutputWriter = new BufferedWriter(fileWriter); d. File file = new File("../file.txt"); BufferedWriter bufferedOutputWriter = new BufferedWriter(file); FileWriter fileWriter = new FileWriter(bufferedOutputWriter );
  • Q Mention the family, order of Squeaker Fish?
  • Q Put off for a future time
  • Q Narsingh Baitha passed away recently was related to which field?
  • Q Can pointers be added? When is it done?
  • Q Create an array in javascript with a list of 4 colors, assign that array to the variable, 'colors'.
  • Q Which two states in the US have towns called Christmas?
  • Q What is the significance of cancel method?
  • Q How To Provide Default Values to Function Parameters?
  • Q Do you have a list of things to do before your 'x' years old?