254

‎Ways of doing a for loop.

Comments
  • 3
    Wow, I actually learnt something today on DevRant. Thanks kind stranger :)
  • 18
    no, I have to do it this way.
    for(i=0;i<n;++i)
  • 8
    for element in array {

    }

    Swift masterrace
  • 10
    map f xs

    Haskell master race.
  • 5
    @isaacWeisberg
    Actually on Swift that loop is:

    for i in 0..10{

    }

    Looks better though than the plain old for loop. But I aint gonna marry my code so doesn't matter lol
  • 7
    Could you please use i++ when you need it? There is no need here, use ++i at least.
    Oh and I fucking forgot, you probably don't need i outside the loop, so int i = 0 will reduce the scope.
  • 7
    That's a bullshit meme. While the number of iterations is the same in both loops, the variable i is not. And you usually need that variable.
  • 3
    What if you really need i start from one? You'll need one more line of code in the for
  • 3
    i = n;
    while( i --> 0 ){
    }
  • 1
    @DustInCompetent which language is that? how do you increase the i value?
  • 1
    @dontbeevil I'm so sick of that face I will find who it is and I will hurt him in such a way he won't stop screaming till he dies.
  • 5
    @dontbeevil seems more like a decrement :p don't know the language though
  • 4
    @dontbeevil it's just while (i-- > 0)
    counts from n backwards and exits when i reaches 0
  • 1
    @Celes oh yeah looks like you're right, I was too focused on the post for :)
  • 4
    please:

    int i = 0;
    for (; i<n;){
    i = i + 1;
    }
  • 2
    n.forEach((x, i) => i);

    Higher order functions ftw.
  • 1
    Anyone remember Smalltalk?
    1 to: n do: [ :i |
    ]
  • 1
    @DustInCompetent oh damn I was so stupid :D the --> confused me
  • 3
    for (i in 0..n) {

    }

    Kotlin rules!
  • 2
    @dannydam So close to Swift, but ya Kotlin is amazing especially as a replacement to Java on Android ...
  • 0
    @dannydam I'm tempted to stop learning java and learn kotlin instead for android programming. Do you think it would be better to learn java and then kotlin? I'd like to skip java if possible but I do understand that it is a useful tool, especially for employment.
  • 1
    @DVZ96 You should continue with Java first and only move to Kotlin when you feel like you understand Java well enough. Java is still the dominant language in Android development, that means that most tutorials use Java and most developers as well. What I'm saying is, Kotlin is great and I recommend learning and using it, but before you do you should have the ability to comfortably switch to Java if necessary.
  • 2
    @badsector c11 and c++ 11 removed the restriction, if I don't say bullshit. Clause 1 can now be a declaration too.

    Edit : https://stackoverflow.com/questions...
    So it was C99, I always mess up about the versions
  • 0
    @dannydam Yeah it seems like a good idea to learn java first. I don't like it that much but it sure is useful to learn android programming.
  • 0
    @hoggchan isn't that code going to loop/count until n-1 elements?
  • 0
    @dontbeevil are you fucking serious?
    Literally any C-alike language
  • 2
    @isaacWeisberg I was confused by
    i --> 0 that actually is i-- > 0 ☺️
  • 1
    Like a boss:
    int n = 0;
    while (!false && n<56.3)
    {
    n++;
    Func(n);
    }
  • 1
    @dontbeevil yeah, yeah, I am sorry, I first wrote a comment and only then saw that you've already seen it)
  • 0
    @gitpush
    Don't you mean:
    for i in 0...<n { ... }
  • 0
    @dannydam
    for (i in n) {

    }

    JavaScript rules! :PP
  • 0
    SPACESSS
Add Comment