Is there a list of already used varible name

I love using the for loop , for (int I_1 = 0; I < a; I++) and I even name the varibles with I_1, I_2, I_3 so I only need to check the last varible to name the next. But sometimes I must add stuff into the middle of the code and all mess up. I cant remember which varible I_(number) I has used which I havent.

Is there a table or something so I can easily check if the varible has been used or not ?

Thanks


If you only use those, although in my opinion the choice for their name is not good, since they all look alike and don't really speak to you when you read them, in for loops, their scope should be relatively short, and I assume that it's very unlikely that you're going to use three nested loops within one loop.

I don't know if that's what you're asking but the following syntax is correct and allowed. Honestly you rarely need more than two int names for 'counting' through for loops'. I personally use int count for my first loop, and int index for the second one.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//int count doesn't exist int the scope here
const int MAX_NUMBER = 5;

for (int count = 0; count < MAX_NUMBER; count ++)
{
    //int count exists and is defined in this scope
    //int index doesn't exist in this scope

    for (int index = 0; index < MAX_NUMBER; index ++)
    {
        //int index exists in this scope
    }

    //int index doesn't exist anymore, you can create a new for loop with a new one
    for (int index = 0; index < MAX_NUMBER; index ++)
    {
        //new int index created
    }
}

//int count doesn't exist anymore in this scope 
//int index doesn't exist anymore in this scope  


I hope this makes things clearer for you
Last edited on
ok cool so you mean I dont need to use I_1 and I_2 but one I for mutiple Loops is enough.
thank I will try that
This question is not clear to me. Please show us an example of what you currently have that you don't like, and we can try to refactor it for you.

In for loops, there really isn't anything wrong with using "i" as the variable name, in most cases. People will understand what it means. H00G0's scope example is good.

If you need to use a for loop within a for loop, consider "j". But only use short names like these as simple, obvious counters. If you are using them for anything other than this, use better names. If your code is more confusing than that, I strongly suggest splitting it up into a functions and (usually) not having more than 2x layers of for loops per function. But it really depends.
Last edited on
variables die out of scope.
for(int i = 0; i < 10; i++);;;;
//i does not exist here. if it does, its either declared again outside the for loop or the compiler has a bug.

there are documenting tools and a compiler as well could tell you all the used variable names, where they came from, etc. I used to use one called 'understand cpp' but its a pay-for tool. Haven't needed this kind of thing in a while.


Hello thejogger1998,

To put what everyone else has said a little differently.

1
2
3
4
5
6
7
8
9
10
11
12
13
constexpr size_t MAXSIZE{ 10 };

int nums[MAXSIZE];

for (size_t i = 0; i < MAXSIZE; i++)
{
	nums[i] // code that may use "i".
}

for (size_t i = 0; i < MAXSIZE; i++)
{
	nums[i] // code that may use "i".
}

This was generated by my IDE the partial code and comment I added. "i" can be used in both for loops without any problem because of the scope of the for loop. The scope of each for loop is different thus making "i" a local variable to each for loop until the closing } is encountered and the loop ends.

For a nested for loops you could do something like:
1
2
3
4
5
6
7
for (size_t i = 0; i < MAXSIZE; i++)
{	
	for (size_t j = 0; j < MAXSIZE; j++)
	{

	}
}

"i" is visible until the closing } of the outer for loop so it is visible inside the inner for loop. As H00G0 likes to use "count" and "index" for the loop iterators if I am dealing with a 2D array I like to use "row" and "col" as it makes the code easier to understand. The point is that you are not limited to just "i" and "j".

I do not know why or when using "i" and "j" became popular. It may be similar reasoning as "Daylight Savings Time" when actually the true form is "Daylight Saving Time". The point being that so many people have added the "s" to "Saving" that it just has become accepted as normal. The same may be true for using "i" and "j" in for loops because it is easy to type and the variable only has scope until the for loop ends.

If you need to you can read about "scope" here http://www.cplusplus.com/doc/tutorial/namespaces/

Hope that helps,

Andy
i and j and k were popular because one of the earliest languages from which C borrowed a few things was fortran, and in fortan original, variable names that started with i, j, or k were integers by default, other letters were double by default. so i/j/k became the variables of choice for loop counters. At least I believe this had some bearing on it. There could be other factors for the choice as well.
Last edited on
i, j, and k have been popular variable names in mathematics when writing summations for a very long time.

https://en.wikipedia.org/wiki/Summation#Capital-sigma_notation
Peter87 wrote:
i, j, and k have been popular variable names in mathematics when writing summations for a very long time.

Agreed. So much so that it is common practice in maths, physics and engineering to drop the Σ in the summation and adopt the Einstein summation convention: imply summation whenever there is a repeated index:
https://en.wikipedia.org/wiki/Einstein_notation

There's a huge amount to be said (and some massive brevity in derivations) for writing a scalar product as
aibi (implied sum over i)
and defining matrix multiplication by
(AB)ij = aikbkj (implied sum over k)
Last edited on
@jonnin, Peter87 and lastchance,

Thanks for the input.

Back in the days when I was first learned C I decided on "lc" (short for loop counter) as the iterator for a for loop. At the time I did not notice what others were using and do not recall the teacher saying anything about using "i", "j" or "k".

What I said was just a guess as I have not read anything that would explain the use of "i", "j" or "k".

Andy
Topic archived. No new replies allowed.