In: Computer Science
I'm in a intro to programming class using C++. In some examples in my book, they use the letter ' i ' to do certain things like a 'for loop'. I was looking around the internet and I can't seem to find what that specific letter does. In all the websites I went to about operators it wasn't listed. Do I need to define it? Or just use it like it is?
Example: for(int i = 0; i < 4; i++)
Here, in the case of for loop or in particular any other case, the letter 'i' is just used as a variable always. It has no relevance specifically and isn't any type of keyword /operator to be particular. Just as generally we declare any variable before using it in C++, we declare the 'i' variable also. Although, there isn't any requirement of defining the variable and assigning value to it beforehand and then using it elsewhere. We can simply declare a variable and assign value to it later on as well. The role of the letter 'i' or any other variable as a whole is to just store the assigned data and use it when called or required. So, for instance, here, we can normally declare the variable I before using it in for loop or anywhere else by using the statement int i and then using it in For(i=0;i<4;i++). We can even declare the variable 'i' inside the for loop itself, for example in for(int i=0;i<4;i++), It doesn't make much difference. So, to be precise, in programming in C++ language we have to follow a golden rule of declaring the variable and then using it somewhere blindly. Nothing else matters beyond that i.e. we can declare the variable wherever and we can define it wherever and whenever we want after its declaration.
Hope this helps:). If you still have any doubt, feel free to ask it out in the comment section.