• Bilb!A
    link
    fedilink
    arrow-up
    3
    arrow-down
    5
    ·
    edit-2
    1 year ago

    WTF, I have never used nor seen “j.”

    I don’t usually have to name these variables these days though. Pretty much everything I use has foreach or some functional programming type stuff.

    And like that, the off-by-one mistakes disappear.

    • SaratogaCx@lemmy.world
      link
      fedilink
      English
      arrow-up
      3
      ·
      1 year ago

      It was very common in text books when showing nested loops

      int nWhatTheCount = 0;
      for (int i = 0; i < 10; i++) { 
          for (int j = 0; j < i; j++) { 
              for (int k = 0; k < j; k++) { 
                  for (int l = 0; l < k; l++) { // and on, and on
                      nWhatTheCount++;
                  }
              }
          }
      }
      
    • spauldo@lemmy.ml
      link
      fedilink
      arrow-up
      1
      ·
      1 year ago

      foreach is useful when you don’t need to know the index of something. If you do, conventional i, j, k, etc. are useful.

      A lot of it depends what you’re doing (number crunching, for instance) or if you’re in a limited programming language (why won’t BASIC die already?) where parallel arrays are still a thing.