windows 如何在一个或多个 for 循环中使用现有变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4117115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you use an existing variable within one or more for loops?
提问by ShadowXOR
I'm working my way through Head First C#, and I'm a bit confused on my current exercise. They state:
我正在学习 Head First C#,但对当前的练习有些困惑。他们说:
If you declare a variable inside a for loop--for (int c = 0; ...)--then that variable's only valid inside the loop's curly brackets. So if you have two for loops that both use the variable, you'll either declare it in each loop or have one declaration outside the loop. And if the variable c is already declared outside of the loops, you can't use it in either one.
如果在 for 循环中声明一个变量——for (int c = 0; ...)——那么该变量仅在循环的大括号内有效。因此,如果您有两个都使用该变量的 for 循环,您要么在每个循环中声明它,要么在循环外有一个声明。如果变量 c 已经在循环之外声明,则不能在任何一个循环中使用它。
This sounds contradictory to me, almost like saying you can only use it outside if you declare it outside, but if you declare it outside you can't use it.
这对我来说听起来很矛盾,几乎就像说如果你在外面声明你只能在外面使用它,但是如果你在外面声明你就不能使用它。
So can you, or can't you? I tried declaring c in two separate for loops and it worked fine, but when declaring c outside of the for loops I couldn't find any way to reference the variable c inside both for loops while it's also declared outside, whether I was trying to change its value or not. This isn't required for the exercise, I'm just trying to soak up every bit of knowledge I come across and trying to go beyond the material.
那你能,还是不能?我尝试在两个单独的 for 循环中声明 c 并且它工作正常,但是当在 for 循环之外声明 c 时,我找不到任何方法来引用两个 for 循环中的变量 c 而它也在外部声明,无论我是否试图改变它的价值与否。这不是练习所必需的,我只是想吸收我遇到的每一点知识并试图超越材料。
The book may be confusing me, so if this isn't possible and is completely unnecessary, just let me know, thanks!
这本书可能让我感到困惑,所以如果这不可能并且完全没有必要,请告诉我,谢谢!
回答by Oded
The issue is one of scoping. Read herefor some details on how variable scoping works in C#.
问题是范围界定之一。阅读此处了解有关变量作用域如何在 C# 中工作的一些详细信息。
If a variable is declared outsidea loop, you can't re-declareit inside:
如果变量在循环外声明,则不能在循环内重新声明:
BAD:
坏:
int c = 0;
for(int c = 0; c < list.Count; c++) // Error!
{
}
OK:
好的:
Declared outside, used inside:
在外面声明,在里面使用:
int c = 0;
for(c = 0; c < list1.Count; c++)
{
}
for(c = 0; c < list2.Count; c++)
{
}
Declared insidetwo loops:
在两个循环中声明:
for(int c = 0; c < list1.Count; c++)
{
}
for(int c = 0; c < list2.Count; c++)
{
}
回答by Philipp
You can either do
你可以做
int i;
for (i = 0; i < 3; i++)
foo(i);
for (i = 0; i < 5; i++)
bar(i);
or
或者
for (int i = 0; i < 3; i++)
foo(i);
for (int i = 0; i < 5; i++)
bar(i);
but not
但不是
int i;
for (int i = 0; i < 3; i++) //error
foo(i);
for (int i = 0; i < 5; i++)
bar(i);
回答by Arief
The statement is indeed confusing, if I understand it correctly, according to the text I should not be able to do this:
声明确实令人困惑,如果我理解正确,根据文字我应该无法做到这一点:
int i;
for (i = 1; i < 10; i++) { }
for (i = 0; i < 20; i++) { }
But I can, and this is clearly valid. I think what the text means is "you can't re-declareit in either one" instead of "you can't use it in either one".
但我可以,而且这显然是有效的。我认为文本的意思是“你不能在任何一个中重新声明它”而不是“你不能在任何一个中使用它”。
回答by TJB
The concept here is Scope. A variable is declared within some scope and cannot be accessed outside of it. This helps define the lifetime of a variable as well as control access to it. A variable can be declared within a class, a method, or a conditional scope within a method such as within an if statement or a for loop.
这里的概念是Scope。变量在某个范围内声明,不能在其外部访问。这有助于定义变量的生命周期以及控制对它的访问。变量可以在类、方法或方法内的条件范围内声明,例如在 if 语句或 for 循环内。
One easy way to think of scope is that you can access a variable
within the pair of curly braces { ... }
it lives under.
考虑作用域的一种简单方法是,您可以访问变量所在
的一对花括号内的变量{ ... }
。
Here's an example:
下面是一个例子:
public class TestClass
{
int p; // p's is in the 'TestClass' scope
public void TestFunction1()
{
Console.WriteLine(p); // OK, p in class scope
// a lives in the 'TestFunction' scope
int a = 1; // Declared outside of any loop.
for (int i = 0; i < 10; i++)
{
// i lives in the scope of this for loop
Console.WriteLine(i);
// a is still accessible since this for loop is inside TestFunction1
Console.WriteLine(a);
}
Console.WriteLine(a); // OK, in scope
//Console.WriteLine(i); // Error, i out of scope
// j also lives in the TestFunction1 scope
int j = 0;
for (j = 0; j < 20; j++)
{
// j still accessible within the loop since the loop is within TestFunction1
Console.WriteLine(j);
}
Console.WriteLine(j); // Ok, j still in scope (outside of loop)
}
public void TestFunction2()
{
Console.WriteLine(p); // Ok, TestFunction2 is in the TestClass scope like TestFunction1
//Console.WriteLine(a); // Error, a lives in TestFunction1
//Console.WriteLine(i); // Error, allright now we're just getting silly ; )
}
}
回答by user2716500
You can use an existing variable as the starting point of a for loop.
您可以使用现有变量作为 for 循环的起点。
I just started learning C# 4 weeks ago, so be weary.. but the syntax is:
我 4 周前才开始学习 C#,所以要厌倦.. 但语法是:
int x = 10;
for (x = x ; x < 15; x++) // x starts off as whatever defined above (15)
{
Console.WriteLine("x is: {0}", x);
}
// After for is done executing, x will = 15
Then for whatever reason, you can continue doing some other logic (When X < 30, something else happens) ex)
然后无论出于何种原因,您都可以继续执行其他一些逻辑(当 X < 30 时,会发生其他事情)例如)
for (x = x ; x < 30; x++)
{
// Do stuff here
}
回答by Wilco
x=x works to re-use the variable keeping its value from one loop to another but it gives you a warning. you could prefer this syntax
x=x 可以重复使用变量,将其值从一个循环保存到另一个循环,但它会给您一个警告。你可以更喜欢这种语法
for (x+=0; x>10; x++) ;
回答by Pieter van Ginkel
I think they mean the following.
我认为它们的意思如下。
You can do this. What happens hear is that you've declared a variable outside of the loop and are using it. The problem though is that you may overwrite an existing value which you need to use somewhere else.
你可以这样做。听说你已经在循环外声明了一个变量并正在使用它。但问题是您可能会覆盖需要在其他地方使用的现有值。
int i = 0;
for (i = 0; i < 100; i++)
{
// Do something
}
What you really can't do is this. Here you are re-using the variable from the outer for
in the inner for
.
你真正做不到的是这个。在这里,您将for
在内部重新使用来自外部的变量for
。
for (int i = 0; i < 100; i++)
{
for (i = 0; i < 100; i++)
{
// Do something
}
}