Explain this to me please; For Loops

I don't understand the output based off this code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #include <iostream>
using namespace std;

void main()
{
	int i, temp[10];
	
	for(i = 0; i < 10; i++)
		temp[i] = 2 * i;
	cout << temp[i];
	for (i = 0; i < 10; i++)
		cout << temp[i] << " ";
	cout << endl;

	for (i = 0; i < 10; i = i + 2)
		cout << temp[i] << " ";
}


output
1
2
-8589934600 2 4 6 8 10 12 14 16 18
0 4 8 12 16 Press any key to continue . . .


I don't understand why the output isn't
1
2
0246810121416180 1 2 3 4 5 6 7 8 9
0 2 4 6 8 10 12 14 16 18 Press any key to continue . . .


And then if I comment out the first for loop the rest of my code goes to hell. I'm confused.
Last edited on
You need to use these things: {}

1
2
3
4
5
for (int i = 0; i < 10; i++)
{
    temp[i] = 2 * i;
    cout << temp[i];
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
using namespace std;

int main() // <- fixed this for you... main should always return int
{
    // These just create some variables.
    // The single integer 'i' and an array of 10 integers, 'temp'
    int i, temp[10];
	
    // This loop properly fills the array with:
    //    0 2 4 6 8 10 12 14 16 18
    //  as you'd expect
    for(i = 0; i < 10; i++)
        temp[i] = 2 * i;
    
    // However, at this point... now i==10 because that's where the last for
    // loop left it.  So this is attempting to print temp[10]... which
    // is OUT OF BOUNDS.  temp[9] is the highest legal index.  [10] is past
    // the end of your array.  Therefore this is accessing random memory and
    // is printing garbage:
    cout << temp[i];
    
    // This properly prints each element in the array as you'd expect:
    //  0 2 4 6 8 10 12 14 16 18
    for (i = 0; i < 10; i++)
        cout << temp[i] << " ";

    // Print a new line
    cout << endl;

    // This will print even elements of the array:
    //   0 4 8 12 16
    for (i = 0; i < 10; i = i + 2)
        cout << temp[i] << " ";
}



EDIT:

To explain further....

A for loop is constructed of several parts. Specifically, 4 parts:

1
2
3
4
for( initialization; condition; increment )
{
    body;
}


It works like this:
1) 'initialization' is performed
2) 'condition' is checked. If it's true, continue to step 3. Otherwise (if it's false), exit the loop.
3) Perform the 'body'
4) Perform the 'increment'
5) go to step #2



For purposes of example... let's look at your last for loop in your code:
1
2
    for (i = 0; i < 10; i = i + 2)
        cout << temp[i] << " ";


The 4 parts are:

1
2
3
4
i = 0;                      // <- initialzation
i < 10;                     // <- condition
i = i + 2;                  // <- increment
cout << temp[i] << " ";     // <- body 


This means the for loop will accomplish the following:

- Step 1 - initialzation. i=0; is performed initializing i.
- Step 2 - check the condition. i<10 is true, so we continue to step 3
- Step 3 - body. temp[i] is printed (since i=0, this prints temp[0], which is 0)
- Step 4 - increment. i = i + 2. After this i==2
-- go back to step 2
- Step 2 - check condition. i<10 is still true, so keep going
- Step 3 - body. i=2, so temp[2], or 4 is printed.
- Step 4 - increment. After this, i==4
-- go back to step 2
- Step 2 - i==4, so condition is still true
- body. temp[4] or 8 is printed
- increment. now i==6
-- loop
- Step 2, condition is still true
- body. temp[6] or 12 is printed
- increment. now i==8
-- loop
- Step 2, condition is still true
- body. temp[8] or 16 is printed
- increment. now i==10
-- loop
- Step 2, i==10, so now the condition i<10 is FALSE... so the loop exits. Leaving i==10
Last edited on
Thanks Disch. Very clear explanation. It would be so much easier if the text books did exactly what you just did to explain the loop variations in the step-by-step format. You should write a textbook real quick ;). I was doing

step 3 -> increment i
step 4 -> body

rather than

step 3 -> body
step 4 -> increment i

but if it was ++i would that be how it is run?

Thanks again
Last edited on
Also I guess I was thinking that the array values would be changed as in the second loop

1
2
3
4
5
temp [0] = 0
temp [1] = 1
temp [2] = 2
...


clearly this isn't so but why and is there a condition when it is?
never mind I get it it would have to look like

1
2
3
{
temp [i] = i
}


right?
but if it was ++i would that be how it is run?


No. Nothing can change that order. That's just how for loops work. It's how they all work.

it would have to look like [snip] right?


Yes, that would give you temp[0]=0, temp[1]=1, etc.
Topic archived. No new replies allowed.