473,549 Members | 2,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

scope question :

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

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

"NotYetaNur d" <No*********@Ma trix.com> wrote in message
news:ec******** ******@TK2MSFTN GP10.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.

"NotYetaNur d" <No*********@Ma trix.com> wrote in message
news:ec******** ******@TK2MSFTN GP10.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
"NotYetaNur d" <No*********@Ma trix.com> wrote in message
news:ec******** ******@TK2MSFTN GP10.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.

"NotYetaNur d" <No*********@Ma trix.com> wrote in message
news:#1******** ******@TK2MSFTN GP10.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
"NotYetaNur d" <No*********@Ma trix.com> wrote in message
news:ec******** ******@TK2MSFTN GP10.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.MainF orm'

"Champika Nirosh" <no****@textcen tric.lk> wrote in message
news:uK******** ******@TK2MSFTN GP10.phx.gbl...
that is since

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

Nirosh.

"NotYetaNur d" <No*********@Ma trix.com> wrote in message
news:#1******** ******@TK2MSFTN GP10.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
"NotYetaNur d" <No*********@Ma trix.com> wrote in message
news:ec******** ******@TK2MSFTN GP10.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.

"NotYetaNur d" <No*********@Ma trix.com> wrote in message
news:#a******** ******@tk2msftn gp13.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.MainF orm'

"Champika Nirosh" <no****@textcen tric.lk> wrote in message
news:uK******** ******@TK2MSFTN GP10.phx.gbl...
that is since

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

Nirosh.

"NotYetaNur d" <No*********@Ma trix.com> wrote in message
news:#1******** ******@TK2MSFTN GP10.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
"NotYetaNur d" <No*********@Ma trix.com> wrote in message
news:ec******** ******@TK2MSFTN GP10.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*********@Ma trix.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8
Champika Nirosh <no****@textcen tric.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #9
Champika Nirosh <no****@textcen tric.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.WriteLi ne (i);
}

{
int i=10;
Console.WriteLi ne (i);
}

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

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

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

Similar topics

3
6453
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 within the Global Namespace. And that, all identifiers within a given namespace, however deeply nested they are, each of them has the Global Scope. ...
6
3144
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 function-prototype scope I think(might be wrong):
5
2074
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 function-prototype scope I think(might be wrong):
8
3362
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 actual program. To me "scope" of the name of a function or object are the general rules for the areas of a program that can through a declaration, have...
3
3426
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 {9, 11} work fine. Why the '.' "scope operator" is accepted in line 9 and not in line 6?
39
3153
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 function changes the state of the object, it ought to be a member of that object." Reference Accelerated C++, A. Koenig, page 159.
7
2171
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
2572
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, introduced, used, potentially evaluated and accessible. They all seem to have subtle difference in meanings. Any positive contribution to the...
1
25631
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 bad breath)? Scope describes the context in which a variable can be used. For example, if a variable's scope is a certain function, then that...
1
3749
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: - "function prototype scope" for structures and unions? - "extern" for internal linkage ?
0
7524
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7451
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7720
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7960
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7475
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7812
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5372
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1061
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.