Why should we only use i in for loop?

Why not?

AFAIK using i in loops stems back to the early days of Fortran (late 1950's) where variables i j k l m n were integer variables by default. The other letters were real numbers.
There's no rule that says you have to.

Using i for a loop variable is a throwback to the early FORTRAN days when the first letter of your variable determined it's type.

Here are some situations where you don't want to use i as a loop variable.

1) Nested loops.

2) When the loop variable represents something obvious like a row or column.

3) When you use std containers such as std::vector, you will probably want to use something more specific such as iter to indicate the loop variable is an iterator.

4) When using the keyword auto, You will want your loop variable to represent an object of the iteration.


i,j, and k are longstanding convention/tradition names. You can, and should, replace them when you have a better name for the value.
Last edited on
Why should we only use i in for loop?

I don't, because I prefer every variable name to self-document what I'm doing.

For example:
for (size_t loop_itr { }; loop_itr < max_value; ++loop_itr)

loop_itr self-documents what the variable is doing. At least to me; hopefully it informs others who read the source as well.

There is no requirement in the standard what a loop's incrementing variable must be named, but there is the long standing tradition of using i (or other single letter variable names ) for the variable.

If you were told it is a requirement that person lied to you.
> There is no requirement in the standard what a loop's incrementing variable must be named,
> but there is the long standing tradition of using i

Most often, the variable involved is of an integer type: for example, an index into an array.
The scopes of names declared in the init-statement (or the condition) of a for loop is usually very small.

Kernighan and Pike in 'The Practice of Programming'
By contrast, shorter names suffice for local variables; within a function, n may be sufficient, npoints is fine, and numberofPoints is overkill.

Local variables used in conventional ways can have very short names. The use of i and j for loop indices ... is so frequent that there is little profit and perhaps some loss in longer names. ...

Programmers are often encouraged to use long variable names regardless of context.
That is a mistake: clarity is often achieved through brevity.


Where the variable involved is not an integer loop index, we wouldn't (shouldn't) use the name i for it.
For example, these are all fairly conventional for loops:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>

struct int_list
{
    struct node { int data = 0 ; node* next = nullptr ; };
    node* head /* = nullptr */ ;

    void print() const
    {
        for( const node* pnode = head ; pnode != nullptr ; pnode = pnode->next )
            std::cout << pnode->data << ' ' ;

        std::cout << '\n' ;
    }

    // other list functions
};

template < typename SEQ > void print( const SEQ& seq )
{
    for( const auto& v : seq ) std::cout << v << ' ' ;

    std::cout << '\n' ;
}

template < typename SEQ > void print2( const SEQ& seq )
{
    const auto end = std::end(seq) ;
    for( auto iter = std::begin(seq) ; iter != end ; ++iter ) std::cout << *iter << ' ' ;

    std::cout << '\n' ;
}

int main()
{
    int cnt = 0 ;
    for( char c = 0 ; std::cout << cnt+1 << ". enter a char (Q to quit): " && std::cin >> c && c != 'Q' ; ++cnt );

    std::cout << "you entered " << cnt << " characters before Q\n" ;

}

Last edited on
It's just a traditional way to write code as we started learning like that but it is not necessary to use 'i' in for loop you can use other variables too
Topic archived. No new replies allowed.