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.
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
- For Fortran66, 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.
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 appearancei
and j
do not tell which is the row index and which is the column index from the variable nameThere 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.
© 2024 OneMinuteCode. All rights reserved.