Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for loop versus foreach

I am fairly new at programming still, and I'm having a hard time been wishing for different between for/foreach. In a more technical exclamation, the for each loop counts how many times it is needed-unlike the code below.

Let's say I have been a sample of a for loop like this:

var i = result = 0;
for (var i = 2; i < process.argv.length; i++) {
    result = result + Number(process.argv[i]);
}

console.log(result);

How would this code change if it were a foreach statement? Is the explanation right? Or is there more to it?

Thank you in Advance.

like image 333
Tryah85 Avatar asked Jan 04 '14 03:01

Tryah85


People also ask

Is foreach better than a for loop?

forEach is almost the same as for or for..of , only slower. There's not much performance difference between the two loops, and you can use whatever better fit's the algorithm. Unlike in AssemblyScript, micro-optimizations of the for loop don't make sense for arrays in JavaScript.

What is difference between for loop and foreach loop?

For Loops executes a block of code until an expression returns false while ForEach loop executed a block of code through the items in object collections. For loop can execute with object collections or without any object collections while ForEach loop can execute with object collections only.

Is foreach loop faster than for loop?

The foreach loop is considered to be much better in performance to that of the generic for loop.

Which is better foreach or for loop in Java?

The key difference between for Loop and foreach loop is that the for loop is a general purpose control structure while the foreach loop is an enhanced for loop that is applicable only to arrays and collections.


1 Answers

A foreach loop goes through all the elements in an array and gives them to you one by one without having to do any messing about with an iteration variable with 'i'. For example, you could do this:

var result = 0;
process.argv.forEach(function(element){
   result = result + element;
});

There is a difference between your code and this code: your code skipped the first two elements because 'i' started at 2. This is harder to do in a foreach loop, and if you need that, you should stick to a for loop.

The foreach loop sets up the counters for you. Convenient, but less flexible. It always starts at the first element and ends at the last one. One cool thing to notice is that we also don't have to do anything like '[i]'. The element in the array is pulled out and passed to our function.

In conclusion, a foreach loop is simply a simplified for loop for cases when you need to look at every element in an array.

I personally think the foreach loop in node.js is ugly, since it isn't really a statement, just a function attached to arrays. I much prefer how they look in something like php where they are a part of the language:

$result = 0;
foreach ($process as $element) {
    $result = $result + $element;
}

But that is just personal taste.

like image 148
Damien Black Avatar answered Oct 28 '22 09:10

Damien Black