473,399 Members | 2,478 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,399 software developers and data experts.

Nested Scope

Could someone give me a simple example of nested scope in C#, please?

I've searched Google for this but have not come up with anything that makes
it clear. I am looking at the ECMA guide and trying to understand Goto in
this contect.

PS: This is not homework.
Apr 11 '07 #1
5 3099
AA2e72E wrote:
Could someone give me a simple example of nested scope in C#, please?

I've searched Google for this but have not come up with anything that makes
it clear. I am looking at the ECMA guide and trying to understand Goto in
this contect.
Here is an example from the ECMA-334 C# Language Specification (some
brackets added for clarity):

static void Main(string[] args) {
string[,] table = {
{"red", "blue", "green"},
{"Monday", "Wednesday", "Friday"}
};
foreach (string str in args) {
int row, colm;
for (row = 0; row <= 1; ++row) {
for (colm = 0; colm <= 2; ++colm)
if (str == table[row,colm]) {
goto done;
}
}
}
Console.WriteLine("{0} not found", str);
continue;
done:
Console.WriteLine("Found {0} at [{1}][{2}]", str, row, colm);
}
}

"a goto statement is used to transfer control out of a nested scope"

Source:
http://www.jaggersoft.com/csharp_standard/15.9.3.htm
--
Göran Andersson
_____
http://www.guffa.com
Apr 11 '07 #2
From the C# ECMA Specification:

class Test
{
static void Main(string[] args)
{
string[,] table = {
{"red", "blue", "green"},
{"Monday", "Wednesday", "Friday"}
};
foreach (string str in args)
{
int row, colm;
for (row = 0; row <= 1; ++row)
for (colm = 0; colm <= 2; ++colm)
if (str == table[row, colm])
goto done;
Console.WriteLine("{0} not found", str);
continue;
done:
Console.WriteLine("Found {0} at [{1}][{2}]", str, row, colm);
}
}
}

In the example above, there are several nested for loops. Each of these has
a local scope that includes all code inside the loop. goto statements are
typically used to break out of nested loops, and in the example above, the
goto statement exits the innermost loop and in fact, its' target is outside
the topmost for loop. This makes the code simpler than using a break
statement in all 3 loops. However, you cannot use a goto statement to ENTER
a loop that is nested inside the scope in which the goto statement resides.
For example, you could not use a goto statement in the outermost loop to
enter any of its' nested loops.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"AA2e72E" <AA*****@discussions.microsoft.comwrote in message
news:E5**********************************@microsof t.com...
Could someone give me a simple example of nested scope in C#, please?

I've searched Google for this but have not come up with anything that
makes
it clear. I am looking at the ECMA guide and trying to understand Goto in
this contect.

PS: This is not homework.


Apr 11 '07 #3
Thanks for the example.

Am I correct in thinking that the 2-D array table has 'nested' scope?
Apr 11 '07 #4
AA2e72E wrote:
Thanks for the example.

Am I correct in thinking that the 2-D array table has 'nested' scope?
That depends on what you mean. The array itself only has the scope of
the method where it's declared. It's the usage of two for loops to
traverse the array that creates a scope inside another scope.

The relevance of the nested scope is that you have to use the goto
command to exit both scopes, while using the break command only would
exit the inner scope. (There are of course other ways of solving this
that would not require a goto.)
It still surprises me every time I move to a new language that the goto
command still exists. I think that it's at least 15 years since I used
it the last time...

--
Göran Andersson
_____
http://www.guffa.com
Apr 11 '07 #5
It still surprises me every time I move to a new language that the goto
command still exists. I think that it's at least 15 years since I used it
the last time...
I know why it's there, but I agree that it should (at least almost) never be
used. It is used occasionally to break out of deeply-nested loops, but even
when that happens, it is usually as a result of poor code design on the part
of the developer.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"Göran Andersson" <gu***@guffa.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
AA2e72E wrote:
>Thanks for the example.

Am I correct in thinking that the 2-D array table has 'nested' scope?

That depends on what you mean. The array itself only has the scope of
<snip>
--
Göran Andersson
_____
http://www.guffa.com

Apr 12 '07 #6

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

Similar topics

6
by: Andy Baker | last post by:
Hi there, I'm learning Python at the moment and trying to grok the thinking behind it's scoping and nesting rules. I was googling for nested functions and found this Guido quote:...
4
by: KInd | last post by:
Hello All, When is nested class more preferable that Inheritance ? I think with proper inheritance and friend class concept we can get the same flexibility as nested classes Any comments .. Best...
3
by: jena | last post by:
Hi I have code # BEGIN CODE def test(): def x(): print a a=2 # *** a=1
9
by: Javaman59 | last post by:
Using local declarations within a block often makes code more readable, but is it less efficient? eg... void P() { while (...) { int i = ...; bool b = ...; .... } }
37
by: Tim N. van der Leeuw | last post by:
Hi, The following might be documented somewhere, but it hit me unexpectedly and I couldn't exactly find this in the manual either. Problem is, that I cannot use augmented assignment operators...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
4
by: Wolfgang Draxinger | last post by:
If you know languages like Python or D you know, that nested functions can be really handy. Though some compilers (looking at GCC) provide the extension of nested functions, I wonder, how one...
0
by: Maric Michaud | last post by:
Le Tuesday 12 August 2008 11:29:18 Cousson, Benoit, vous avez écrit : This is a language limitation. This is because nested scope is implemented for python function only since 2.3 allow late...
0
by: Cousson, Benoit | last post by:
This is a language limitation. That was my understanding as well, but I think it is a pity to have that limitation. Don't you think that the same improvement that was done for method nested scope...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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...
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
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,...
0
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...

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.