Why do programmers always start with variable I,j,k for loops and not a,b, c ...?

  • 17
    i is for index the j, k, l... are just the letters that follows i
  • 0
    In python package quantarhei we often need a, b, c for bra and ket states. We use on the other side i, j, k anywhere you have no idea what short name you should use.
  • 10
    I always think of “index” or “iteration”
  • 5
    I have always thought that the "i" stands for "iterator"
  • 3
    To be fair, `a` stands for accumulator.
  • 3
    It's what's common to use for the index variable in math in general as well. You are free to use any other symbol, but people might look for the reason why you are specifically avoiding this convention.

    I remember using 'o' (for object) for the lambda variable in a project where everyone else just used x (or 'e' for element/event in the frontend), just because x and e incurred autocompletes for me. I ended up renaming it all back to x and e after discovering how it looks like I'm signalling something by deliberately changing the notation.

    I figure the third "no it's fine, just checking" was my call to just follow convention. Funny how much time these things can cost.
  • 0
    @bosi The things you learn after programming for 10 years....

    But, I regularly use a and b as well when working with nested loops!
  • 1
    I use i, j, b, and n for some reason. Probably got it from a textbook somewhere.

    j is probably not good though, error prone.
  • 0
    Here's my current logic:
    i is an iterator or index,
    v, *v are vectors or collections,
    *c is the number of elements in the respective *v,
    el,elem,item are elements,
    ev is an event

    e is avoided due to it being inobvious.

    If there are multiple of any of these, none of them may have a type-based name. If you know the implementation, every name must be obvious individually, and type-based names are only obvious when there's only one value of the type involved.
  • 2
    In math, abc often denote constants. For example f(x) = ax² + bx + c
  • 0
    There's no way you have two nested loops and no distinct property by which to name both iterators, so j is already a bad name.
  • 0
    I do also use X, Y Z in some cases
  • 2
    The "i for index" is retrofitted. It actually comes from Fortran, where variables had default types inferred from their names unless the type was explicitly declared. If a variable name started with I,J,K,L,M or N, it was integer, otherwise it was real.
  • 1
    How about no longer use indexed loops (if your language allows it) and call your variables what they are.

    So don't do
    for(int i=0; some boolean; i++) {

    And instead do this
    for(var billingItem in basket) {

    Much more readable and safer to use 😁
  • 0
    @gronostaj i for index exists in math in general
  • 0
    @Yggdrasil except when you want to know the index for other reasons
  • 0
    no idea, i do c for a single cycle, c1, c2, c3... for nested cycles, and x, y, z, when they're about traversing (pseudo)spatial structures.
  • 0
    Ugly ( a, b, c )

    Cool ( i, j, x )

    *There’s also a difference in width.
  • 0
    @amoux What kind of monster doesn't use monospace?
  • 0
    Is that your favorite font?
Add Comment