displayData

Pages: 12
I hear you ..I just don't know how to assign the condition and increment to main function to repeat. My study guide refers to repeating a number by different increments. I've read this over and over and it just does not make sense to this. Please could you just explain the order in doing this..I would appreciate.
I don't get it. You've already show in your own posts in this thread that you can write a for loop that loops a set number of times. Just do the same thing as you yourself have written in some of your previous posts. You can't have suddenly lost all the understanding you had earlier today!
Gosh....

when having to loop a number and increase it by 2 every time I do understand that. But in this case I do not know

I do not know what to enter in the start expression. When the program displays the desirable data it needs to start over.

Then the condition is it must start at the first function in main.

Then the increments must be by 5 runs....

Could you please just type the loop out if you do actually know. I should straight away understand and it will clarify much more.

Lets assume that you want to repeat something for N times. (We don't care what the "something" is.)

For the loop we want to have a counter to keep track how many times we have already done the something.

Initialization is what is done before the loop. We set the initial value for the counter. If the counter says how many times the loop's body has already been executed, what should be the value when the loop's body has been executed no times at all?

Condition is an expression that returns bool. It should evaluate to true as long as the times we have repeated is less than how many times we want to repeat.

Increment happens after each time the body of the loop has been executed; at the end of each iteration. Since the counter is used here to hold a value that tells how many times the body has been executed so far, it should increment by one after execution of the body.

If you have
1
2
3
4
int main() {
  // some code
  return 0;
}

and convert it to:
1
2
3
4
5
6
int main() {
  if ( init ; cond ; incr ) {
    // some code
  }
  return 0;
}

You do achieve the repetition of the // some code , if your init, cond, incr are appropriate.


I will not "just type" for you, because you might not reach enlightenment that way. I want you to think.
Thank you for the explanation but I don't think anyone understand why im struggling with this
 
for (int i = 0; i >5; ++i )


1) why use i or j ?
2) what is i..a general variable that will automatically refer to the code added?
3) can i be used in any loop ?
4) does i automatically become the condition of the loop function when values added ?

Please go read all literature online or books and see that they always refer to a certain value or character

comparing a while loop:
1
2
3
4
5
6
7
8
9
10
int i = 1
while (i <=10)
cout <<"line" 1++<<i<<endl;
i++

this is simple  vs for 

for (int i = 1; i <= 10; i++)
cout << "Line " << i << endl;


simple it displays line 10 times in a row

but how do I assign something else than a number to i?
1) why use i or j ?

Why not? You can call it whatever you want, but often people use i for a counter. If you want to use another name, that's fine.

2) what is i..a general variable that will automatically refer to the code added?


i is an integer variable. It holds a number, that's an integer. Here, we're just using it to count the number of iterations, that's all.

I don't understand what you mean by "a general variable that will automatically refer to the code added". Can you clarify that, please?

3) can i be used in any loop ?

Yes. You can name a variable whatever you like, within the general rules of variable naming in C++. There are no special rules about what names you can use in a loop.

does i automatically become the condition of the loop function when values added ?

I don't quite understand what you mean by that. Nothing automatically becomes the condition of the loop. You specify what the condition is, in your for statement.

In this case, you've specified that the condition is i <= 10.

how do I assign something else than a number to i?

The same way you'd do it for any other variable. You declare the variable to be the right type for the data you want to store in it.
Last edited on
Thank you for the explanation but I don't think anyone understand why im struggling with this
for ( int i = 0; i >5; ++i )

1. The "i" is created set to 0 before the loop
2. The condition in true, if "i" is larger than 5. However, the "i" starts equal to 0 and 0 > 5 is false. Therefore, the loop's body will not execute even once. No iteration. No incrementation. The execution jumps to the next statement that is after the loop.

Your other example has both syntax and logical problems:
1
2
3
4
5
6
7
8
9
10
11
int i = 1 // no semi-colon at the end of statement
while (i <=10)

cout << "line" 1++ << i << endl; // this is the only statement in the loop's body
// The "line" 1++ is a very strange looking literal constant, or an error

i++ // no semi-colon at the end of statement


for (int i = 1; i <= 10; i++)
cout << "Line " << i << endl;
1
2
for (int i = 1; i <= 10; i++)
cout << "Line " << i << endl;

simple it displays line 10 times in a row

but how do I assign something else than a number to i?

You're definitely not getting the way for loops work. You don't need to assign something else to i. Here, i is just a variable used to control how many times the loop runs. If you have this:
1
2
3
for (int i=1; i <= 10; i++) {
    ...  // anything here executes 10 times.
}

Then you can replace ... with whatever code you want. The loop will cause the code to execute 10 times. If you want it to execute 20 times, all you have to do is change the condition:
1
2
3
for (int i=1; i <= 20; i++) {
    ...  // now anything here executes 20 times.
}


Does this make sense? Now lets return to your original code. You had this in your main function:
1
2
3
    getData();
    calculateVolume();
    displayData();


If you want to execute this code 5 times then all you have to do is put that code inside a for loop like the one above.

Does this make sense?
Last edited on
Thank you guys ..I do understand now that "i" has nothing to do with my program. That its just a variable or counter symbol for the used loop. This is what was throwing me off the track. Feel a bit stupid now...thnx Hayden
the first part is telling me the value of the variable then the execution states when it should stop and then how it should be repeated. In the main body the loop function refers to WHAT exactly should be executed. In my case the body of my main function...
Topic archived. No new replies allowed.
Pages: 12