473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

scope question :

for(int i=0;i<6;i++)
{
//
}

wont i go out of scope after the for loop ...?
Nov 15 '05 #1
23 1679
Yep.

"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:ec**************@TK2MSFTNGP10.phx.gbl...
for(int i=0;i<6;i++)
{
//
}

wont i go out of scope after the for loop ...?

Nov 15 '05 #2
yes you go ... that is only from the things you have define within the scope
of the for loop

Nirosh.

"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:ec**************@TK2MSFTNGP10.phx.gbl...
for(int i=0;i<6;i++)
{
//
}

wont i go out of scope after the for loop ...?

Nov 15 '05 #3
when i tried this
for(int i=0;i<6;i++)
{
for(int j=0;j<someConst;j++)
{
}
}

int i=1;
foreach(.......)
{
asdf=--i;
}

I am getting the following error

"A local variable named 'i' cannot be declared in this scope because it
would give a different meaning to 'i', which is already used in a 'child'
scope to denote something else
"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:ec**************@TK2MSFTNGP10.phx.gbl...
for(int i=0;i<6;i++)
{
//
}

wont i go out of scope after the for loop ...?

Nov 15 '05 #4
that is since

for (int i=0; i<6; i++) === int i; for(i=0; i<6; i+)

Nirosh.

"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:#1**************@TK2MSFTNGP10.phx.gbl...
when i tried this
for(int i=0;i<6;i++)
{
for(int j=0;j<someConst;j++)
{
}
}

int i=1;
foreach(.......)
{
asdf=--i;
}

I am getting the following error

"A local variable named 'i' cannot be declared in this scope because it
would give a different meaning to 'i', which is already used in a 'child'
scope to denote something else
"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:ec**************@TK2MSFTNGP10.phx.gbl...
for(int i=0;i<6;i++)
{
//
}

wont i go out of scope after the for loop ...?


Nov 15 '05 #5
in that case
following should work rite

for(int i=0;i<6;i++)
{

}
i = 1;
it gives the following error
The name 'i' does not exist in the class or namespace 'JTaskMgr.MainForm'

"Champika Nirosh" <no****@textcentric.lk> wrote in message
news:uK**************@TK2MSFTNGP10.phx.gbl...
that is since

for (int i=0; i<6; i++) === int i; for(i=0; i<6; i+)

Nirosh.

"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:#1**************@TK2MSFTNGP10.phx.gbl...
when i tried this
for(int i=0;i<6;i++)
{
for(int j=0;j<someConst;j++)
{
}
}

int i=1;
foreach(.......)
{
asdf=--i;
}

I am getting the following error

"A local variable named 'i' cannot be declared in this scope because it
would give a different meaning to 'i', which is already used in a 'child' scope to denote something else
"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:ec**************@TK2MSFTNGP10.phx.gbl...
for(int i=0;i<6;i++)
{
//
}

wont i go out of scope after the for loop ...?



Nov 15 '05 #6
Yes that won't work and let me explain it this way

Even if the scope of an element is limited to the block, its lifetime is
still that of the entire procedure. Therefore when you define the int i in
the procedure it give a different meaning to the variable i define in the
child scope.

but if you define your new i in a another block that will prevent you from
the reported error

Nirosh.

"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:#a**************@tk2msftngp13.phx.gbl...
in that case
following should work rite

for(int i=0;i<6;i++)
{

}
i = 1;
it gives the following error
The name 'i' does not exist in the class or namespace 'JTaskMgr.MainForm'

"Champika Nirosh" <no****@textcentric.lk> wrote in message
news:uK**************@TK2MSFTNGP10.phx.gbl...
that is since

for (int i=0; i<6; i++) === int i; for(i=0; i<6; i+)

Nirosh.

"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:#1**************@TK2MSFTNGP10.phx.gbl...
when i tried this
for(int i=0;i<6;i++)
{
for(int j=0;j<someConst;j++)
{
}
}

int i=1;
foreach(.......)
{
asdf=--i;
}

I am getting the following error

"A local variable named 'i' cannot be declared in this scope because it would give a different meaning to 'i', which is already used in a 'child' scope to denote something else
"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:ec**************@TK2MSFTNGP10.phx.gbl...
> for(int i=0;i<6;i++)
> {
> //
> }
>
> wont i go out of scope after the for loop ...?
>
>



Nov 15 '05 #7
NotYetaNurd <No*********@Matrix.com> wrote:
when i tried this
for(int i=0;i<6;i++)
{
for(int j=0;j<someConst;j++)
{
}
}

int i=1;
foreach(.......)
{
asdf=--i;
}

I am getting the following error

"A local variable named 'i' cannot be declared in this scope because it
would give a different meaning to 'i', which is already used in a 'child'
scope to denote something else


Indeed. The problem is that the "int i=1;" declaration has a scope for
its entire block - including the for loop which occurs before it.
Effectively, you've got:

int i;

for (int i=0; i < 6; i++)
{
....
}

i=1;
foreach(....)
{
....
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8
Champika Nirosh <no****@textcentric.lk> wrote:
that is since

for (int i=0; i<6; i++) === int i; for(i=0; i<6; i+)


No it's not. See another reply for a fuller explanation.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #9
Champika Nirosh <no****@textcentric.lk> wrote:
Yes that won't work and let me explain it this way

Even if the scope of an element is limited to the block, its lifetime is
still that of the entire procedure.


No it's not. It's the lifetime of the *block*. This, for instance
compiles fine:

using System;

class Test
{
static void Main()
{
{
int i=5;
Console.WriteLine (i);
}

{
int i=10;
Console.WriteLine (i);
}

{
for (int i=0; i < 5; i++)
{
Console.WriteLine (i);
}
}
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #10
What proof you have to say that the the variable (which define in a block)
lifetime is not equal to that of the entire procedure.

your example is not capable of talking about the life time and I am not
talking about the SCOPE.

Nirosh.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Champika Nirosh <no****@textcentric.lk> wrote:
Yes that won't work and let me explain it this way

Even if the scope of an element is limited to the block, its lifetime is
still that of the entire procedure.


No it's not. It's the lifetime of the *block*. This, for instance
compiles fine:

using System;

class Test
{
static void Main()
{
{
int i=5;
Console.WriteLine (i);
}

{
int i=10;
Console.WriteLine (i);
}

{
for (int i=0; i < 5; i++)
{
Console.WriteLine (i);
}
}
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #11
if it has the scope for the entire block the following should compile rite

{
for (int i=0; i < 6; i++)
{
...
}
i=1;
}
but am getting

" The name 'i' does not exist in the class or namespace......."

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
NotYetaNurd <No*********@Matrix.com> wrote:
when i tried this
for(int i=0;i<6;i++)
{
for(int j=0;j<someConst;j++)
{
}
}

int i=1;
foreach(.......)
{
asdf=--i;
}

I am getting the following error

"A local variable named 'i' cannot be declared in this scope because it
would give a different meaning to 'i', which is already used in a 'child' scope to denote something else


Indeed. The problem is that the "int i=1;" declaration has a scope for
its entire block - including the for loop which occurs before it.
Effectively, you've got:

int i;

for (int i=0; i < 6; i++)
{
...
}

i=1;
foreach(....)
{
...
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #12
If i defined in the procudure has the scope for its entire procedure I
should be able to do some thing like

i = 0;
int i;

but this is not possible why?

Nirosh.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
NotYetaNurd <No*********@Matrix.com> wrote:
when i tried this
for(int i=0;i<6;i++)
{
for(int j=0;j<someConst;j++)
{
}
}

int i=1;
foreach(.......)
{
asdf=--i;
}

I am getting the following error

"A local variable named 'i' cannot be declared in this scope because it
would give a different meaning to 'i', which is already used in a 'child' scope to denote something else


Indeed. The problem is that the "int i=1;" declaration has a scope for
its entire block - including the for loop which occurs before it.
Effectively, you've got:

int i;

for (int i=0; i < 6; i++)
{
...
}

i=1;
foreach(....)
{
...
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #13
NotYetaNurd <No*********@Matrix.com> wrote:
if it has the scope for the entire block the following should compile rite

{
for (int i=0; i < 6; i++)
{
...
}
i=1;
}
but am getting

" The name 'i' does not exist in the class or namespace......."


No, because the block of the variable is the for loop - that is a block
itself.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #14
Champika Nirosh <no****@textcentric.lk> wrote:
What proof you have to say that the the variable (which define in a block)
lifetime is not equal to that of the entire procedure.

your example is not capable of talking about the life time and I am not
talking about the SCOPE.


So what *exactly* do you mean by lifetime here? (I had thought you'd
just changed terminology, as the rest of the thread is about scope and
your explanation didn't really explain muhc, IMO.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #15
Please Read

http://msdn.microsoft.com/library/de...us/vbcn7/html/
vbconscopelevels.asp

Nirosh

"Champika Nirosh" <no****@textcentric.lk> wrote in message
news:u3**************@TK2MSFTNGP11.phx.gbl...
What proof you have to say that the the variable (which define in a block)
lifetime is not equal to that of the entire procedure.

your example is not capable of talking about the life time and I am not
talking about the SCOPE.

Nirosh.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Champika Nirosh <no****@textcentric.lk> wrote:
Yes that won't work and let me explain it this way

Even if the scope of an element is limited to the block, its lifetime is still that of the entire procedure.


No it's not. It's the lifetime of the *block*. This, for instance
compiles fine:

using System;

class Test
{
static void Main()
{
{
int i=5;
Console.WriteLine (i);
}

{
int i=10;
Console.WriteLine (i);
}

{
for (int i=0; i < 5; i++)
{
Console.WriteLine (i);
}
}
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 15 '05 #16
Champika Nirosh <no****@textcentric.lk> wrote:
If i defined in the procudure has the scope for its entire procedure I
should be able to do some thing like

i = 0;
int i;

but this is not possible why?


Because you can't *use* variables "out of order" - they have that
scope, but you can't assign to or read from them until they've been
declared. I know it's a little odd, but in practice it's rarely a
problem.

(It doesn't have scope for the whole method, btw, only the whole block
it's declared in.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #17
Champika Nirosh <no****@textcentric.lk> wrote:
http://msdn.microsoft.com/library/de...us/vbcn7/html/
vbconscopelevels.asp


That's about VB though, which may well (I haven't checked) have
entirely different rules to C#, which is the language under discussion.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #18
Champika Nirosh <no****@textcentric.lk> wrote:
What proof you have to say that the the variable (which define in a block)
lifetime is not equal to that of the entire procedure.

your example is not capable of talking about the life time and I am not
talking about the SCOPE.


Okay, I think it's time to start looking at what the spec has to say.
ECMA numbering used throughout.

From section 12.1.7:

<quote>
The lifetime of a local variable is the portion of program execution
during which storage is guaranteed to be reserved for it. This lifetime
extends from entry into the block, for-statement, switch-statement, or
using-statement with which it is associated, until execution of that
block, for-statement, switch-statement, or using-statement ends in any
way.
</quote>

So no, not the whole method - just the block it's associated with.

Also from 12.1.7:

<quote>
Within the scope of a local variable, it is a compile-time error to
refer to that local variable in a textual position that precedes its
local-variable-declarator.
</quote>

That addresses your other question about why you can't do

i=1;
int i;

From section 10.7:

<quote>
The scope of a local variable declared in a local-variable-declaration
(§15.5.1) is the block in which the declaration occurs.
The scope of a local variable declared in a switch-block of a switch
statement (§15.7.2) is the switch-block.
The scope of a local variable declared in a for-initializer of a for
statement (§15.8.3) is the for-initializer, the for-condition, the for-
iterator, and the contained statement of the for statement.
</quote>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #19
No I don't think since it is .Net. Rules that are use in CLR are almost same
for both C# and VB.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Champika Nirosh <no****@textcentric.lk> wrote:
http://msdn.microsoft.com/library/de...us/vbcn7/html/ vbconscopelevels.asp


That's about VB though, which may well (I haven't checked) have
entirely different rules to C#, which is the language under discussion.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #20
Agreed, no argument

Can you just read the link I gave and made your comments?

Nirosh.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Champika Nirosh <no****@textcentric.lk> wrote:
What proof you have to say that the the variable (which define in a block)
lifetime is not equal to that of the entire procedure.

your example is not capable of talking about the life time and I am not
talking about the SCOPE.


Okay, I think it's time to start looking at what the spec has to say.
ECMA numbering used throughout.

From section 12.1.7:

<quote>
The lifetime of a local variable is the portion of program execution
during which storage is guaranteed to be reserved for it. This lifetime
extends from entry into the block, for-statement, switch-statement, or
using-statement with which it is associated, until execution of that
block, for-statement, switch-statement, or using-statement ends in any
way.
</quote>

So no, not the whole method - just the block it's associated with.

Also from 12.1.7:

<quote>
Within the scope of a local variable, it is a compile-time error to
refer to that local variable in a textual position that precedes its
local-variable-declarator.
</quote>

That addresses your other question about why you can't do

i=1;
int i;

From section 10.7:

<quote>
The scope of a local variable declared in a local-variable-declaration
(§15.5.1) is the block in which the declaration occurs.
The scope of a local variable declared in a switch-block of a switch
statement (§15.7.2) is the switch-block.
The scope of a local variable declared in a for-initializer of a for
statement (§15.8.3) is the for-initializer, the for-condition, the for-
iterator, and the contained statement of the for statement.
</quote>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #21
Champika Nirosh <no****@textcentric.lk> wrote:
No I don't think since it is .Net. Rules that are use in CLR are almost same
for both C# and VB.


Rules of scope for a variable are *language* rules, not CLR rules.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #22
Champika Nirosh <no****@textcentric.lk> wrote:
Agreed, no argument

Can you just read the link I gave and made your comments?


I'm not sure I see the point, to be honest - that's VB.NET, which could
have entirely different scoping rules. Just because they both target
..NET doesn't mean things have to work exactly the same way.

For instance, you could have a language D# which *did* allow

i=5;
int i;

Why would that have to change what code is generated? The D# compiler
could just rearrange the variable declaration to the start of its
scope, and then call the C# compiler. They would both target .NET, but
would have different rules for how variables could be accessed.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #23
Champika,
for (int i=0; i<6; i++) === int i; for(i=0; i<6; i+) That doesn't sound correct, as I can do:

for(int i=0;i<6;i++)
{

}
for(long i=0;i<6;i++)
{

}

Which does compile & run.

By your statement the following would be equal, which actually causes a
compile error!

int i; for(i=0;i<6;i++)
{

}
long i; for(i=0;i<6;i++)
{

}

As i is already defined to be an int!

I will defer to one of the C# experts as to the "why"!

Hope this helps
Jay

"Champika Nirosh" <no****@textcentric.lk> wrote in message
news:uK**************@TK2MSFTNGP10.phx.gbl... that is since

for (int i=0; i<6; i++) === int i; for(i=0; i<6; i+)

Nirosh.

"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:#1**************@TK2MSFTNGP10.phx.gbl...
when i tried this
for(int i=0;i<6;i++)
{
for(int j=0;j<someConst;j++)
{
}
}

int i=1;
foreach(.......)
{
asdf=--i;
}

I am getting the following error

"A local variable named 'i' cannot be declared in this scope because it
would give a different meaning to 'i', which is already used in a 'child' scope to denote something else
"NotYetaNurd" <No*********@Matrix.com> wrote in message
news:ec**************@TK2MSFTNGP10.phx.gbl...
for(int i=0;i<6;i++)
{
//
}

wont i go out of scope after the for loop ...?



Nov 15 '05 #24

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Anonymous | last post by:
Is namespace the same thing as scope? While reading the book "Thinking in C++", I was under the impression that namespace is, well, a namespace--a feature to create a hiearchy for identifiers...
6
by: pembed2003 | last post by:
Hi all, I am reading the book "C++ How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
5
by: pembed2003 | last post by:
Hi all, I am reading the book "C How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
8
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
3
by: marco_segurini | last post by:
Hi, I am using VS 2005. If I compile the following code only line 6 returns me an error while line 9 returns a warning. If I comment the line 6 and debug the program the assignments of lines...
39
by: utab | last post by:
Dear all, Is there a clear distinction how to decide which functions to be members of a class and which not How is your attitude (Your general way from your experiences ...) "If the...
7
by: Christian Christmann | last post by:
Hi, I've a a question on the specifier extern. Code example: void func( void ) { extern int e; //...
1
by: Steven T. Hatton | last post by:
All of the following terms are used in some way to describe where and how a name is relevant to a particular location in a program: visible, declarative region, scope, potential scope, valid,...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
1
by: Giacomo Catenazzi | last post by:
Hello, To learn the details of C, I've build the following example, could you check if it is correct and if it miss some important cases? Are there some useful (real cases) examples of: -...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.