Nested Scope

G

Guest

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.
 
G

Guest

AA2e72E said:
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
 
K

Kevin Spencer

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
 
G

Guest

Thanks for the example.

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

Guest

AA2e72E said:
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...
 
K

Kevin Spencer

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 said:
That depends on what you mean. The array itself only has the scope of
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top