For loop - Last statement skipped, how execute?

I'm working my way through the tutorials and I came across one of the examples using the "for-loop".

Goofing around with some code, I made a simple program similar to the one in the tutorial... the code is as follows:


//for loop
#include <iostream>
using namespace std;

int main ()

for (n=0, i=100; n!=i; n++, i--)
{
cout <<"n = " <<n <<" " <<"i = " <<i <<endl;}

system ("Pause");
return 0;



My question is... how can I make a statement after the "for statement" to display the final values of n and i (since the final statement in the loop is skipped as a result of n==i)?

My compiler is giving me some goofy error saying:
Line 1. "name look-up of 'n' changed for new ISO 'for' scoping"
Line 2. "using obsolete binding at 'n'

Are these local variables and only accessible in the for loop? Can this be averted by initializing n and i OUTSIDE of the for loop and then leaving the initializing empty in the for loop?

Thanks,
VX
Last edited on
closed account (DSLq5Di1)
No type specified for either n or i, compiler is likely assuming int and warning you about it. You hit the nail on the head in regard to them being local variables only accessible inside the loop. ^^
If you want to display the values of n and i, declare them outside the loop, create a while loop, and do the increment and decrement at the end of the loop.
Awesome, thanks.

Ya, I defined n and i outside the loop and left the initialization blank within the "for loop". After that I was able to do a cout and display the final values without getting the error from my compiler.

Global variable FTW.

Thanks once again,
VX
Last edited on
Topic archived. No new replies allowed.