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

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++ Loop Variable
Click For Summary

Discussion Overview

The discussion revolves around understanding the loop variable [i] in C++ for loops, addressing its purpose, necessity, and how it interacts with other variables within the loop. Participants explore the mechanics of for loops, including iteration and variable incrementing, while also expressing confusion and seeking clarification on these concepts.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about the purpose of the loop variable [i] and questions why it is necessary when other variables could be used.
  • Another participant explains that the loop variable is essential for iterating through elements of an array and performing calculations efficiently.
  • Participants share code examples to illustrate how the loop variable functions within the loop, specifically in relation to accessing array elements.
  • There is a discussion about the incrementing of the loop variable, with some participants asserting that the increment happens before the loop body executes, while others challenge this view, stating it occurs after.
  • A participant compares the loop variable to a calendar, suggesting it helps keep track of the current iteration, while another participant questions the rationale behind not using existing variables for iteration.
  • One participant provides an analogy to clarify the need for a dedicated loop variable, comparing it to tracking dates separately from income in a spreadsheet.
  • Another participant admits to a misunderstanding regarding the incrementing of the loop variable and acknowledges the need for clarity on this topic.

Areas of Agreement / Disagreement

Participants express varying levels of understanding regarding the loop variable and its incrementing behavior. While some agree on its necessity for iteration, there is disagreement about the mechanics of incrementing and whether it occurs before or after the loop body executes. The discussion remains unresolved on these technical points.

Contextual Notes

Participants reference specific behaviors of the loop variable and incrementing that may depend on individual interpretations or coding practices. There is also mention of potential confusion arising from different incrementing styles (pre-increment vs. post-increment) without reaching a consensus on their implications.

Who May Find This Useful

This discussion may be useful for individuals learning C++ programming, particularly those seeking to understand the mechanics of for loops and the role of loop variables in iteration.

ineedhelpnow
Messages
649
Reaction score
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
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?
 
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?
 
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?
 
0, 1... and then one number less than NUM_VALS?
 
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?
 
im a little confused but okay.
 
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.
 
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.
 

Similar threads

  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 34 ·
2
Replies
34
Views
4K
Replies
3
Views
2K
Replies
9
Views
4K