We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,629 Members — Technology Publication meets Social Media

# Why "int i" ?

Well, i'm pretty much a newbie into programming, merely because i'm just 15, but i really like informatics ( specially programing ) but i'm not that wealthy so, i can't pay for private teaching, or anything like that.

But if you want to, you can, that's what i say. I've been learning c++ from a while now ( few months ) but everytime i see some math operation i see the "for (int i<...." For example:

int lower limit,upper limit;
int range=upper limit-lower limit;
int number;
for(int i=0;i<20;i++)
{
number=lower limit+(rand()%range);
cout<<number<<endl;
}

Why the hell is there an i, does it means something, it's just an easy way to remember something or what?.

Sorry for the dumb question, but i'm really an autodidact so it's kinf of hard to get someone to explain me this.

SORRY! AND GREETINGS!

16
Contributors
18
Replies
1 Year
Discussion Span
1 Year Ago
Last Updated
19
Views
pato wlmc
Light Poster
31 posts since Mar 2010
Reputation Points: 10
Skill Endorsements: 0

You could always try for( i=0;i<20;i++) and observe the error messages

Maybe then try

int i;
for(i=0;i<20;i++)
Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,875
Skill Endorsements: 27

You could always try for( i=0;i<20;i++) and observe the error messages

Maybe then try

int i;
for(i=0;i<20;i++)

Well, yeah, that answers one question, but why i , why not a, or u
does that i means something?

pato wlmc
Light Poster
31 posts since Mar 2010
Reputation Points: 10
Skill Endorsements: 0

Why the hell is there an i, does it means something, it's just an easy way to remember something or what?.

Relax and read some Wikipedia -> loop counter

FYI. If you don't like (too) short variable names, you are free to pick your poison. I think, C++ standard recommends that an identifier would be allowed to be 1024 characters long (the minimum).

mitrmkar
Posting Virtuoso
1,834 posts since Nov 2007
Reputation Points: 1,119
Skill Endorsements: 8

i is generally used for loop counters.. but no one is forcing you.
Specially if the body (the part between { and }) of the for loop is big, you might consider using a more meaningful name for the loop counter.

There are a lot of 'naming conventions'.. each with people who like it and people who protest it.

I generally use i if the body is small, else something like iLoopCount or simmilar. In any case... consider what your personal perference is and stick to it (be precise).
I personally have a strict naming convention.. e.g. pointers start with p, a 'char' starts with c, strings with sz, DWORDs with dw(or ul) etc.

thelamb
Posting Pro in Training
426 posts since Aug 2008
Reputation Points: 193
Skill Endorsements: 0

It is a variable, you can use whatever you want

int bacon = 0;

for (bacon = 0; bacon != 20; ++bacon)

is a favourite of mine. However "i" is typically used in for loops. I hate when people declare the integer in the for loop though

for (int i = 0; i !=20; ++i)

that's just awful, declare ALL your variables at the top please.

Acrimonus
Light Poster
25 posts since Jan 2010
Reputation Points: 5
Skill Endorsements: 0

Absolutely not.. there is more wrong with declaring the variable on top than inside the for statement.

It's all to do with scope, if you declare the variable on top of the for loop, it is still in scope when the for loop exits, which is usually not what you want.

Declaring it in the for statement means the variable will go out of scope when the loop ends.

So declaring it on top, without using it anymore after the loop creates confusion for other people reading your code.

thelamb
Posting Pro in Training
426 posts since Aug 2008
Reputation Points: 193
Skill Endorsements: 0

Well thanks jeje, i became with one question, and i got out with a lot of different answers, that just generated even more doubts, but thats the way it's supose to be, isn't it??

Anyway, thanks, and don't worry, you'll probably see me again really soon. :D

pato wlmc
Light Poster
31 posts since Mar 2010
Reputation Points: 10
Skill Endorsements: 0

I would say, that i has origin in iterating. Because you use it to iterate through all its values.

bubblies
Newbie Poster
1 post since Nov 2011
Reputation Points: 10
Skill Endorsements: 0

I believe that 'i' is used in accordance with the rules of Hungarian Notion; a coding style used to self identify the type variables (int type vars use 'i').

Clinton Portis
Practically a Posting Shark
833 posts since Oct 2005
Reputation Points: 237
Skill Endorsements: 5

I believe that 'i' is used in accordance with the rules of Hungarian Notion; a coding style used to self identify the type variables (int type vars use 'i').

Nope. i has been used long before Hungarian Notation.

From Wikipedia:
"The original Hungarian notation, which would now be called Apps Hungarian, was invented by Charles Simonyi, a programmer who worked at Xerox PARC circa 1972-1981, and who later became Chief Architect at Microsoft. It may have been derived from the earlier principle of using the first letter of a variable name to set its type — for example, variables whose names started with letters I through N in FORTRAN were integers by default."

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Skill Endorsements: 37

Nope. i has been used long before Hungarian Notation.

From Wikipedia:
"The original Hungarian notation, which would now be called Apps Hungarian, was invented by Charles Simonyi, a programmer who worked at Xerox PARC circa 1972-1981, and who later became Chief Architect at Microsoft. It may have been derived from the earlier principle of using the first letter of a variable name to set its type — for example, variables whose names started with letters I through N in FORTRAN were integers by default."

i for iterator is my guess. 2nd guess its from i,j,k from vector directions.

Greywolf333
Junior Poster
145 posts since Sep 2010
Reputation Points: 40
Skill Endorsements: 0

Actually, it predates computer programming entirely; it comes from conventions in mathematical notation, where i, j and k are traditionally used to indicate the indices of arrays and the iterative variables of summation (big-sigma) operations.

Schol-R-LEA
Veteran Poster
1,089 posts since Oct 2010
Reputation Points: 495
Skill Endorsements: 13

Index, in fact. It's a long-standing math tradition.
From math it was assumed by Fortran (which is Formula Translator), where anything starting with I, J, K, L, M and N was implicitly integer. Then it became a programming tradition.

nezachem
Posting Shark
913 posts since Dec 2009
Reputation Points: 719
Skill Endorsements: 0

World is so inclined to Apple. Thats why every first one (Phones, tablets & even loop) start with an I....Just joking....

PrimePackster
Posting Whiz in Training
252 posts since Nov 2011
Reputation Points: 21
Skill Endorsements: 3

One of the first real fortran program every written was about the gamma function, and in typical mathematical notation it evaluated $$\sum_{i=1}^{6} \frac{\gamma_i}{1+\lambda_i \tau}$$ ina typical math function. Unfortunately I haven't seen the original code for it. But the use if i,j,... etc is extremely common for subscript in maths notation, so given that it was mathematicians that were the originators of computing, their notation become standard were it is applicable.

However, the first fortran compile manual [IBM 704] (published in 1956), uses i as a loop variable in the very first program (finding the largest number in a set).

StuXYZ
Practically a Master Poster
681 posts since Nov 2008
Reputation Points: 760
Skill Endorsements: 0

frogboy77
Posting Pro in Training
481 posts since Aug 2010
Reputation Points: 100
Skill Endorsements: 1

Hey. Look, it's just a convention.
All you're saying is that I am creating a variable named i which has a data type of int.
The fact that it is so widely used as an example as well as in actual production code just means that it is universally accepted.
As the other contributors say, you don't have to use i.
The fact is that you can not use int because that is a reseved word, so i is the next best thing.
i keeps it short but it's not very meaningful.

Well, i'm pretty much a newbie into programming, merely because i'm just 15, but i really like informatics ( specially programing ) but i'm not that wealthy so, i can't pay for private teaching, or anything like that.

But if you want to, you can, that's what i say. I've been learning c++ from a while now ( few months ) but everytime i see some math operation i see the "for (int i<...." For example:

int lower limit,upper limit;
int range=upper limit-lower limit;
int number;
for(int i=0;i<20;i++)
{
number=lower limit+(rand()%range);
cout<<number<<endl;
}

Why the hell is there an i, does it means something, it's just an easy way to remember something or what?.

Sorry for the dumb question, but i'm really an autodidact so it's kinf of hard to get someone to explain me this.

SORRY! AND GREETINGS!

mlesniak
Junior Poster in Training
50 posts since Jul 2006
Reputation Points: 32
Skill Endorsements: 0

i is used probably because it stands for iteration. People choose to use it because i is short and it looks clean when you use it as an array index (compare array to array[numloops]).

Tumlee
Junior Poster
172 posts since Oct 2011
Reputation Points: 84