16

I was writing something like this code:

do {
    int i = 0;
    int j = i * 2;
    cout<<j;

    i++;
} while (j < 100);

(http://codepad.org/n5ym7J5w)

and I was surprised when my compiler told me that I cannot use the variable 'j' because it is not declared outside the do-while loop.

I am just curious about if there is any technical reason why this cant be possible.

José D.
  • 4,175
  • 7
  • 28
  • 47

5 Answers5

13

The scope of j is just within the {} braces. You can't use it in the loop condition, which is outside that scope.

From a C++ draft standard I have handy:

A name declared in a block is local to that block. Its potential scope begins at its point of declaration and ends at the end of its declarative region.

A "block" is also known as a "compound statement", and is a set of statements enclosed in braces {}.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
7

There is a reason why this can't be possible. It is due to the limitation of "statement-scope".

Your variables i and j have been declared with "local scope" -- that is variables inside {} brackets. You actually wanted j to be declared with "statement scope" but this is not possible.

Statement-scope are those variables declared as part of 'for', 'while', 'if' or 'switch' statements. Statement scope does not cover do-while statements, though, which is why you cannot do this.

You have basically exposed a language drawback of using do-while.

It would be better if the language offered:

do {
.
.
.
} while (int j < 100);

but it does not offer this.

Faisal Memon
  • 2,711
  • 16
  • 30
0

Double, which must be changed to an unreserved word (thanks Chad), needs to be declared outside the do's scope in order to be used in the while condition.

int i = 0;
int d = 0;
do{
    d = i * 2;
    cout << d;
    i++;
} while (d < 100);
user2680198
  • 306
  • 3
  • 4
0
  • The syntax for do loop is do{ } any variable declaration within the { } will only exist within this scope. Therefore the scope of j is only within do{ } because it was declared within this scope. It can only be called or referenced within this scope.
  • If you want to access j outside this scope, you need to declare j as global variable. Move the declaration of j outside this scope.
Juniar
  • 1,269
  • 1
  • 15
  • 24
-1
int j = 0;
for(int i = 0;j < 100;++i){
j = i * 2;
cout<<j;
}

before every loop set i = 0; and declarate as int + for loop works better

  • Care to explain? Code without explanations is not acceptable on SO. Also, please indent. -1 – syam Aug 30 '13 at 20:59
  • im bad english, just i try to help and getting -1 realy... – Michał Szałapski Aug 30 '13 at 21:00
  • Yes -1 because you didn't provide any explanation. It doesn't matter if your english is not very good, but please try at least. How are we supposed to understand what you mean if you just give code and no explanation at all? I know this seems harsh but this is how we keep improving both SO and ourselves. – syam Aug 30 '13 at 21:02
  • 1
    No problem, just remember that my vote was not against **you** but just against this one **message**. I removed it, because you tried. ;) – syam Aug 30 '13 at 21:05