Why do you prefer i,j,... as variable names in the loop?

Asked 2 years ago, Updated 2 years ago, 43 views

Why do you prefer i,j,... as the variable name of the loop?
Default? International rules?

 for (inti=0;i<N;i++);

c++ c

2022-09-30 18:38

3 Answers

We use the integer i in English integer, and i is also the initial of index, and we often use i,j,k for variables such as mathematical 法 notation.


2022-09-30 18:38

Variables for loops are
- Integer type should be used (not floating point type)
- Should be a local variable
- By deliberately shortening the name, make it easier to understand that it is a variable for loop retention.
There is an implicit agreement that

There is a disagreement about which comes first, but
- 66, variables with the first character of the variable name being IJKLMN are implicitly integers
- Since it is an integer type, use this name to hold the number of loops
There is a custom in which the name ijk is used as a variable for loops.

Since i is the first letter of index or integer, it is almost certain that

There seems to be no explicit "rules."
It's just a custom, but it's better to follow the custom.


2022-09-30 18:38

It probably derives from the use of i, j, k... in mathematics, from which it is imported into programming and such practices are widely accepted.There are no such rules, so you don't have to follow them.

In addition to subscripts, most notation in programming languages is derived from mathematics.For example, adding parentheses to arguments in the application of functions like f(x) is of course mathematical.It is unnatural to think that the same notation is derived from Fortran, which is already widely used in mathematics, so I personally don't think it comes from Fortran.

This is just a custom, so you don't have to follow it if you have a reason.Rather, even if it is a temporary variable limited to a short scope, for readability, it is preferable to use meaningful variable names rather than meaningless variables such as i and j.If you're a programming beginner, you've been told at least once that you shouldn't use single-character variables.For example,

inti,j;
intm[] = {0,1,2,3};

for(i=0;i<2;i++){
    for(j=0;j<2;i++){
        cout<<m[2*j+i]<<";";
    }
    cout<<endl;
}

In fact, I mistyped i where j should be written in this double for loop.

  • i and j are so similar that it's hard to tell them apart by their appearance
  • The names
  • i and j do not tell which is the row index and which is the column index from the variable name
  • And these two characters are next to each other on the keyboard, so it's easy to type incorrectly
  • There is only one character, so even if I make a mistake, I can't find it due to a compilation error

There are many disadvantages.Use meaningful variable names, such as rowIndex and columnIndex to

introIndex, columnIndex;
intm[] = {0,1,2,3};

for(rowIndex=0;rowIndex<2;rowIndex++){
    for (columnIndex=0; columnIndex<2;rowIndex++) {
        cout<<m[2*rowIndex+columnIndex]<";";
    }
    cout<<endl;
}

As shown in , it is easy to check even if you look at it, and even if you type one letter incorrectly, there is no risk of compiling as it is.It is true that this kind of custom is accepted, but apart from the fact that math is different, I think it is a bad habit in programming.You shouldn't follow these bad habits easily, but they are rarely fatal and are not often considered problematic in reality.I also use i and j without much concern even though I think it's a bad habit.If you are meticulous, you should stop i and j and give them a meaningful name.


2022-09-30 18:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.