-1

when i run the following c++ code

#include<iostream>

int main()
{
   for(int i=1;i<=10;i++)
   {
       cout<<i<<endl;
   }
   cout<<i;

   getch();
   return 0;
}

I get result from no. 1 to 11.

i don't understand why the value of i = 11 after the block of for loop is finished,Please give me the reason.I have declared i inside for loop and scope of i has been finished after loop so why i get the outpout i=11 after execute second cout statement .I have not declared i in the variable declaration inside main.My question is that is i is visible outside of for loop? Thanks in advance.

arpit
  • 555
  • 1
  • 7
  • 25
  • 10
    This seems very odd to me. Your program shouldn't be compiling. `i` shouldn't exist after the loop is done. – Omnifarious Nov 17 '11 at 05:36
  • 1
    I think @Omnifarious comment is better answer than the answers posted here! – niko Nov 17 '11 at 05:40
  • What compiler and version are you using? – Miguel Grinberg Nov 17 '11 at 05:40
  • I think turbo c compiler :) seeing the getch() :P – niko Nov 17 '11 at 05:42
  • 1
    @niko: Oh, **that** would explain it. That is a very, very old compiler. Now I understand. I had forgotten about the bios `getch` function on the old MS-DOS platform. That's 20 years old now! I can't believe anybody even still has the disks (and they'd have to be disks!) for it! – Omnifarious Nov 17 '11 at 05:56
  • i think you are not getting my question i have edit it plz check it. – arpit Nov 17 '11 at 06:58
  • @arpit: That statement should not even have compiled, much less caused the output of '11'. On any modern C++ compiler I know of that code would result in an error. Which compiler are you using? – Omnifarious Nov 17 '11 at 07:28
  • @arpit: I tried to change my answer to more directly address what seems to be your question. – Omnifarious Nov 17 '11 at 20:13

4 Answers4

4

For multiple reasons, this program does not compile. You are either using an extremely old compiler, an extremely permissive compiler, or not showing us the program you're actually having a problem with.

From your comments it seems that you actually can compile it. I can only guess that you are using a very old compiler. Perhaps an old MS-DOS compiler (Zortech C++? Turbo C++?) since the getch function is not generally a standard library function and doesn't do the right thing in the curses library anyway. It's probably an old BIOS-based function from the MS-DOS days.

The standard was changed awhile ago (over 10 years now) so that variable declarations in the parenthesized section of a for loop are local to that loop. It was once not actually the case that this was true.

I no longer have access to any compiler that's so old it doesn't handle things this way. I'm surprised anybody does. Your program will not compile on my compiler.

Here is a version of your program that does compile, even though it requires the -lcurses option to link:

#include <iostream>
#include <curses.h>

using ::std::cout;
using ::std::endl;

int main()
{
    for(int i=1;i<=10;i++)    
    {
        cout<<i<<endl;
    }

    getch();
    return 0;
}

Notice how the offending cout << i; statement is gone? That because it will not compile on a modern compiler.

Now, lets edit your program some more so it will compile with the cout << i; statement you're vexed about:

#include <iostream>
#include <curses.h>

int main()
{
    using ::std::cout;
    int i;

    for (i = 1; i <= 10; i++)
    {
        cout << i << '\n';
    }
    cout << "last: " << i << '\n';

    getch();
    return 0;
}

This, of course, does print out last: 11 at the very end. This happens for a very obvious reason. What value does i have to have in order for the i <= 10 test to fail? Why, any value greater than 10! And since i is having one added to it every loop iteration, the first value i has that has the property of being greater than 10 is 11.

The loop test happens at the top of the loop and is used to decide if the remainder of the loop should be executed or not. And the increment happens at the very bottom of the loop (despite appearing in the body of the for statement). So i will be 10, will be printed, and then 1 will be added to it. Then the loop test (i <= 10) will be done, it will be discovered that 11 <= 10 is false, and control will drop out of the loop down to the print statement after the loop, and last: 11 will be printed.

And yes, the exact same thing will happen in C.

Omnifarious
  • 54,333
  • 19
  • 131
  • 194
1

Because the loop breaks when the condition i<=10 becomes untrue, and this can happen when i becomes 11. Simple!

I think you wanted to write i < 10 .

Also, as @Omnifarious noted in the comment, the code shouldn't even compile, as i doesn't exist outside the loop. Maybe, you've declared i outside the loop, in your original code?

Nawaz
  • 353,942
  • 115
  • 666
  • 851
1

Besides the fact that it shouldn't compile (because i doesn't exist outside the loop block).

The loop runs from 1 to 10, so it stops when i reaches 11 and the condition fails. So i is 11 in the end of the loop.

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
1

This is because you have an old compiler.

cout<<i<<endl; should not compile, as cout and endl need to be qualified by the std namespace.

Fixing that, std::cout<<i; shouldn't compile because your variable is loop-scoped, so shouldn't even be visible outside the loop.

Fixing that, here's your code:

#include<iostream>
#include<conio.h>

int main()
{
    int i;
    for(i = 1; i <= 10; i++)
    {
        std::cout << i << std::endl;
    }

    std::cout << i;

    getch();
    return 0;
}

It should become more obvious why 11 is printed now.

When i == 10, the loop executes, increments i, and checks its value. It is then equal to 11, so it exits the loop.

Then you have another print statement, that will print the post-loop value, which is 11.

Here's the output I get from that corrected program:

1
2
3
4
5
6
7
8
9
10
11

This is the same as you are getting.

If you only want to print 1-10, then why have the extra std::cout << i;?

Recommendation

  • Get an up to date C++ compiler that will give you syntax errors on things that are no longer valid in standard-compliant C++
  • Get rid of the extra std::cout << i;
  • Keep your i variable loop-scoped, as in your original code

The result from my recommendations will be that you only see 1 through 10 printed, and you will have slightly fewer unexpected surprises in the future (as some "bad" and surprising code won't even compile).

Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
  • BTW, `conio.h` might be a windows header. Not sure. `getch` looks like it is from the curses library (?) – Merlyn Morgan-Graham Nov 17 '11 at 05:46
  • 1
    +1 - This is a very complete explanation. But I always clean up code to remove the stupid and useless `endl` statement. It should never have been put in the `iostream` library in the first place. It's a horrible waste and the cause of much mysteriously slow code and weird misconceptions about the portability of `'\n'`. See: http://stackoverflow.com/questions/2122986/why-does-endl-get-used-as-a-synonym-for-n-even-though-it-incurs-significant-p – Omnifarious Nov 17 '11 at 06:07
  • @Omnifarious: Thanks for the links. Good reads, that direction :) – Merlyn Morgan-Graham Nov 17 '11 at 06:13