Archived

This topic is now archived and is closed to further replies.

index????

This topic is 5305 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi i am reading a book that doesnt explain what the variable(?) index is or does nor does it say that it will ever explain in a later chapter. i wanted to see if i could get an answer siince im practicing right now and would like to experiment wiith as much as i have been expected to learn here''s some code..
for (; index < 10; index++)
     cout<< index << " ";
     cout<< endl;
ok that pretty much tells me that index will be incremented till it reaches ten but whats been declared????? and also this one the lette i????
#include<iostream>
using namespace std;

int main(void)
{
     cout << "A multiplication table:" << endl
          << "  1\t2\t3\t4\t5\t6\t7\t8\t9" << endl
          << "  ------------------"
          << "-----------------------"
          << "-----------------------" << endl;
     for(int c = 1; c < 10; c++)
     {
          cout << c << "| ";
          for(int i = 1; i < 10; i++)
          {
          cout << i * c << ''\t'';
          }
          cout << endl;
     }
     return 0;
}
the i in there hasnt been declared either!! ehat the heck makes index and i so special??? i have to be missing something.. "Hello World!!!"

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
i is declared. In the line reading:
for(int i = 1; i < 10; i++)
i is declared as an int. index must have been declared before the line that you posted, but I assure you it was declared. There is nothing special about i or index, just convention. They could have called the variables bob and horseplop. i and index are usually used as counters because that''s what programmers have been calling them for years, that''s it, no magic.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
for(int i = 0; i < 10; i++)

i is declared in the first parameter of the for loop int i = 0;

this could also be written like this

int i = 0;
for(i = 0; i < 10; i++)
however it is good practice to declare your indexers in the loop they are used so you don''t get mixed up and you can use i in more that one loop more easily
does that clear things up?

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
i is declared. In the line reading:
for(int i = 1; i < 10; i++)
i is declared as an int. index must have been declared before the line that you posted, but I assure you it was declared. There is nothing special about i or index, just convention. They could have called the variables bob and horseplop. i and index are usually used as counters because that''s what programmers have been calling them for years, that''s it, no magic.


ok my mistake with i but i still dont understand what that thing is with the for loop and index. whats this about?
for (; index < 10; index++)

theres nothing to declare before the ";".. so how did index get declared?


"Hello World!!!"

Share this post


Link to post
Share on other sites
Look up higher in the code. It should be initialized somewhere above. Otherwise it is just sloppy code. The compiler may or may not initialize it to something when it is first declared, but relying on that would be a poor programming practice.

Share this post


Link to post
Share on other sites
ok cool i just copied that peice from my book there as nothing more to it just that it was a small example of the for loop
so i guess the declared it somewhere i understand that now but i : with nothing before it still loses me
that musta been a mistake also. i hope.

"Hello World!!!"

Share this post


Link to post
Share on other sites