"Understanding the C++ For Loop Variable [i]"

In summary, the for loop allows you to iterate a block of code a pre-defined number of times, where the index variable is needed to address elements of an array, or to be used somehow in calculations. The first time the loop is iterated, the block of code within the for loop is equivalent to: if (valsVctr.at(1) < tempVal) { tempVal= valsVctr.at(1); } The second time: if (valsVctr.at(2) < tempVal) { tempVal= valsVctr.at(2); } And the last time: if (valsVctr.at(NUM_VALS) < tempVal) { tempVal
  • #1
ineedhelpnow
651
0
ok so i realized my problem with c++ is that i don't really understand for loops. well it's not exactly the loop itself but i don't understand the loop variable . what is for EXACTLY? why is it needed? why can't you just use the other variable you are using? why does this extra variable need to be added?
 
Technology news on Phys.org
  • #2
The for loop allows you to iterate a block of code a pre-defined number of times, where the index variable is needed to address elements of an array, or to be used somehow in calculations.

For example, suppose you want to print the natural numbers from 1 to 1000. Now, you could write 1000 lines of code to print each number, OR, you could use a for loop:

Code:
for (var i = 1; i < 1001; i++)
{
    printf("%i \n",i);
{

Much more efficient, wouldn't you say?
 
  • #3
Code:
tempVal = valsVctr.at(0); 
for (i = 0; i < NUM_VALS; ++i) {
   if (valsVctr.at(i) < tempVal) {
      tempVal= valsVctr.at(i);
   }
}
sometimes your given something like this. i get a headache just by looking at this. what's the purpose of the i?
 
  • #4
ineedhelpnow said:
Code:
tempVal = valsVctr.at(0); 
for (i = 0; i < NUM_VALS; ++i) {
   if (valsVctr.at(i) < tempVal) {
      tempVal= valsVctr.at(i);
   }
}
sometimes your given something like this. i get a headache just by looking at this. what's the purpose of the i?

What is the value of [m]i[/m] the first time the loop is iterated?, The second time...the last time?
 
  • #5
0, 1... and then one number less than NUM_VALS?
 
  • #6
ineedhelpnow said:
0, 1... and then one number less than NUM_VALS?

Because the [m]++[/m] operator comes before the index variable in the loop definition, it is incremented before the loop is iterated, rather than after, so the first time it is 1, the second time it is 2 and the last time it is equal to [m]NUM_VALS[/m].

So, do you see how the value of [m]i[/m] affects the block of code within the loop?
 
  • #7
im a little confused but okay.
 
  • #8
ineedhelpnow said:
im a little confused but okay.

The first time the loop is iterated, the block of code within the for loop is equivalent to:

Code:
   if (valsVctr.at(1) < tempVal) {
      tempVal= valsVctr.at(1);

The second time:

Code:
   if (valsVctr.at(2) < tempVal) {
      tempVal= valsVctr.at(2);

And the last time:

Code:
   if (valsVctr.at(NUM_VALS) < tempVal) {
      tempVal= valsVctr.at(NUM_VALS);

The for loop allows a very efficient way of having this block of code executed [m]NUM_VALS[/m] times, without having to write the block [m]NUM_VALS[/m] times.
 
  • #9
ineedhelpnow said:
well it's not exactly the loop itself but i don't understand the loop variable . what is for EXACTLY? why is it needed? why can't you just use the other variable you are using?
Surely you must realize that we don't know what this other variable you are talking about.

ineedhelpnow said:
what's the purpose of the i?
Its purpose is for the body of the loop to know which iteration of the loop it is currently executing. How else would the loop know that during the $i$th iteration it is supposed to work, for example, on the $i$th element of the array? Without the counter that contains the number of the current iteration the program will have to do the same work in every iteration (not exactly, but that's the idea). The counter to a loop is like a calendar to a person.

MarkFL said:
Because the [m]++[/m] operator comes before the index variable in the loop definition, it is incremented before the loop is iterated, rather than after, so the first time it is 1, the second time it is 2 and the last time it is equal to [m]NUM_VALS[/m].
Actually, incrementing is done after the execution of the loop body. The value of the incrementing expression is thrown away, if I understand correctly, so there is no difference between [m]++i[/m] and [m]i++[/m] in the third part of the [m]for[/m] loop. See StackExchange.
 
  • #10
i meant if a variable was already defined or something say for example numCars or yellowFeet. i don't understand why u don't just increment on those values and why u define the loop variable.
 
  • #11
ineedhelpnow said:
i don't understand why u don't just increment on those values and why u define the loop variable.
Each variable stores its own information. What you are saying sounds a little like, "Why do I need a special place to write the current date? Why don't I use my day's income for that? So tomorrow I'll increment my income because it's the next date." So if today you earned \$100, tomorrow you change it to 101. Then you earn \$150, so you rewrite 101 to \$150, and on the following date you change it to 151. Are you imagining something like this? And if you don't know the current date, how to do you know in which cell in the spreadsheet you should record your income? OK, it may be the first free cell, but what if the previous day was a holiday and you did not work? Should you write there because it's the first free cell?
 
  • #12
ok, i understand now :) thanks (Blush)
 
  • #13
Evgeny.Makarov said:
...
Actually, incrementing is done after the execution of the loop body. The value of the incrementing expression is thrown away, if I understand correctly, so there is no difference between [m]++i[/m] and [m]i++[/m] in the third part of the [m]for[/m] loop. See StackExchange.

Okay, I will admit I have always used [m]i++[/m] in the incrementation of the index in a loop definition, so I naturally that [m]++i[/m] would have incrementation done before iteration...that's what I get for assuming.

Also, the code posted by the OP makes less sense for this incrementation to be done after iteration.
 

FAQ: "Understanding the C++ For Loop Variable [i]"

What is the purpose of the for loop variable [i] in C++?

The for loop variable [i] is used as a counter to keep track of the number of times the loop has executed. It is typically used to access elements in an array or to iterate through a sequence of numbers.

What is the initial value of the for loop variable [i]?

The initial value of the for loop variable [i] is defined in the for loop declaration. It can be any integer value, but it is usually set to 0 or 1.

How does the for loop variable [i] change during each iteration of the loop?

The for loop variable [i] is incremented or decremented by the loop's increment expression at the end of each iteration. This allows it to keep track of the number of times the loop has executed.

Can the for loop variable [i] be used outside of the for loop?

Yes, the for loop variable [i] can be used outside of the for loop, but its value will only be valid if it is declared outside of the loop. If it is declared within the loop, its value will not be accessible outside of the loop's scope.

Can the for loop variable [i] be modified within the loop?

Yes, the for loop variable [i] can be modified within the loop. This can be useful for creating nested loops or for implementing complex looping logic.

Similar threads

Replies
16
Views
2K
Replies
2
Views
2K
Replies
4
Views
1K
Replies
4
Views
954
Replies
34
Views
3K
Replies
11
Views
1K
Back
Top