0
for(int i = 0 ; i<n ; i++) { do something }

has equivalent while loop :

int i = 0;
while(i<n) {
  do something
  i++;
}

Now,

for(int i = 4 ; false ; );
print i;//prints 4
for(int i = 7 ; false ; );
print i;//prints 7

according to my understanding, must be equivalent to :

int i = 4;
while(false);
int i = 7;
while(false);

which is two times declaration for 'i'. And, obvious error.

Now, Why there is no redeclaration error for 'i' in for loops?

code ran after giving two warnings :

warning: name lookup of 'i1' changed for new ANSI 'for' scoping

warning: using obsolete binding at 'i1'

2 Answers2

2

No They are not equal

for(int i = 4 ; false ; ); // scope of i is within for 
print i; // This is a compilation error
for(int i = 7 ; false ; ); // scope of i is within for loop
print i; // This is a compilation error

Where as here

int i = 4; // scope is not strictly blocked hence 
while(false);
int i = 7; // re declaration error for this
while(false);
melpomene
  • 84,125
  • 8
  • 85
  • 148
asio_guy
  • 3,667
  • 2
  • 19
  • 35
  • isn't `while(false)` an infinite loop, and makes the system crash? – Nasr Sep 13 '15 at 13:12
  • @Nasr Infinite loops won't make a crash. BTW, that loop isn't an infinite loop. It won't even run once. Were you thinking of `while(true)`? – Spikatrix Sep 13 '15 at 13:16
  • @CoolGuy Yes you are right, misread here :) but infinite loop running on OS will make a crash (Depending on the OS), right? – Nasr Sep 13 '15 at 13:20
  • @Nasr I don't know it exactly, but AFAIK, no. Also, I think that infinite loops like `while(true);` will be removed by modern compilers to enhance performance. – Spikatrix Sep 13 '15 at 13:24
  • 1
    @Nasr infinite loop won't make the system crash, it just runs **forever**. However if each iteration it requests for more resources like memory, disk space... undefined behavior will happen, which may result in a crash. Just try some simple infinite loop and you'll see that the program only takes 100% of a CPU. – phuclv Sep 13 '15 at 13:54
0

Microsoft made an extension for their compilers, if enabled, the scope of for loop variables is extended to the outer scope, and the example would not compile. This is not standard compliant anymore, it's a historical relict. The extension can be disabled in the project settings.

alain
  • 11,939
  • 2
  • 31
  • 51
  • 3
    [https://msdn.microsoft.com/en-us/library/84wcsx8x.aspx](https://msdn.microsoft.com/en-us/library/84wcsx8x.aspx) – 4566976 Sep 13 '15 at 13:18
  • @4566976 so it is some settings error? if i make the settings right. it will not compile? – user3104225 Sep 13 '15 at 13:25
  • @user3104225 As you use gcc this is not relevant. It's for Microsofts compiler cl.exe. – 4566976 Sep 13 '15 at 13:33
  • @4566976 I am not sure if it is Microsoft's cl compiler. I am using Microsoft OS, so probably yes. – user3104225 Sep 13 '15 at 13:36
  • @4566976 If you dont mind, i just have one more thing to ask. isn't it obvious that a variable is set to retain it's value after the loop, if it is declared outside(before) for loop? Then, why there is need to mention that "index variable i retains it's value when the loop terminates" – user3104225 Sep 13 '15 at 13:39
  • I think it's just a bit an unlucky wording for saying "the scope is extended". – alain Sep 13 '15 at 13:53