Programming often involves working on redundant tasks. The for loops help shorten the code and reduce tedious tasks. But the way for is used can be...
For further actions, you may consider blocking this person and/or reporting abuse
Love the apples-to-apples comparison.
Here's Ruby:
But in practice, nobody uses for loops.
Here's a while:
But nobody uses those either.
People are way more likely to do
Or using a collection of objects, like an array or an ActiveRecord collection.
Thank you for adding Ruby syntax. This post was written a few year ago when I found myself always scrambling to write loop when working with different programming languages. Nowadays, a functional programming approach is preferable. I love Python and Ruby for their clean syntax.
Do you mind if I include your Ruby code into the post? I will put credit where it belongs.
Go for it. And no need to credit if it effects the reading in any way. 🙂
There's a few other ways to iterate in JavaScript:
for-of
forEach
Functional programming - map
Note:
forEach(console.log)
will print the item, index and the array - developer.mozilla.org/en-US/docs/W...There is no need to use breckets in lambda:
Here's one for Go:
You can also use a while loop:
Note how a "while loop" is basically the same as a for loop.
Or in rust
In Python, this:
or this:
It's why i use Python.
Your PHP string foreach example seems wrong - PHP strings can be referenced like an array. The following works on PHP, for example.
I did include that too, but it was down. Now i moved it to the top. Thanks man.
Let's be fair with Java. The language has seen quite a few enhancements since Java 8 release, 3 years ago, and very recently on Java 9, both on API and syntax. Now using the new stuff...
Loop a string:
Loop a collection:
Nice post! When working with JavaScript/TypeScript, I usually go for the
forEach
array method, which is pretty convenient for usage with functions:If using a library like lodash, you can also iterate over objects (though iteration order is not guaranteed):
Here's the ways to loop a string and an array in C# with a for loop, they're pretty much the same as any other language.
However, C# really likes iterators, so those tasks are most commonly done with a foreach loop.
Then resharper tells you that you can replace that with one line of linq... if my experience is anything to go by 😀
Swift 2, 3, 4:
I never learn/write Swift. Look interesting. Thanks for sharing.
Very nice, but it's missing my favorite, C++!
Note I'm using the prefix increment in my for loop, for pedantic (and habitual) performance reasons. (In reality, your compiler usually optimizes this itself.)
Given more time later, I'll come back and add the other two.
I built a website ago just to keep how different programming languages approach concepts in their different ways.
Like the Fibonacci in 6 languages: code.khophi.co/codes/-KL7i0Vj9WSsh...
code.khophi.co
I checked it out. That's a good idea. Keep it up.
Java 9 IntStream has a new static method:
iterate(int seed, IntPredicate hasNext, IntUnaryOperator next)
is equivalent to the following for-i loop, but more in a functional style
Here's in Smalltalk:
this works for collections, strings, and objects all the same :D
Awesome. One size fits all. :-)
The wonders of dynamically typed languages 😊
PHP code doesn't have syntax highlighting though 😀
I guess it is not support here yet. That's not fair for PHP. :-)
I'm not quite sure why that is. Definitely looking into it.