Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# For循环-如果布尔条件的计算结果为false,则继续_C# - Fatal编程技术网

C# For循环-如果布尔条件的计算结果为false,则继续

C# For循环-如果布尔条件的计算结果为false,则继续,c#,C#,我正在编写一个包含多个if语句的for循环 如果for语句中的if语句(或其中的一部分)的计算结果为false,那么循环不会退出,而是整数以1的增量迭代并继续通过循环(我需要像continue;关键字这样的功能) 例如: for (int i = 0; i <= Collection.Count && Collection[i].Name != "Alan"; i++) { // If name is not Alan, increment i and contin

我正在编写一个包含多个if语句的for循环

如果for语句中的if语句(或其中的一部分)的计算结果为false,那么循环不会退出,而是整数以1的增量迭代并继续通过循环(我需要像continue;关键字这样的功能)

例如:

for (int i = 0; i <= Collection.Count && Collection[i].Name != "Alan"; i++)
{
    // If name is not Alan, increment i and continue the loop.
} 

for(int i=0;i您需要像
continue
关键字这样的功能-那么您考虑过使用
continue
关键字吗

更新:您的示例代码很难理解您的意图

for (int i = 0; i <= Collection.Count && Collection[i].Name != "Alan"; i++)
{
    // If name is not Alan, increment i.
} 
您可以将
Where
扩展方法视为筛选集合的一种方法

如果这看起来不清楚,您可以使用
continue
关键字(如您所猜测的)实现相同的效果:

或者,您可以将代码放入
if
的块中:

foreach (var item in Collection)
{
    if (item.Name != "Alan")
    {
        // item is not an "Alan"
    }
}
你是说像这样吗

for (int i = 0; i < 100; ) {
    if (!condition1) {
        i++;
    }
    if (!condition2) {
        i++;
    }
    if (!condition3) {
        i++;
    }
}
for(int i=0;i<100;){
如果(!条件1){
i++;
}
如果(!条件2){
i++;
}
如果(!条件3){
i++;
}
}

你想让完成for循环的增量在循环体中吗?

我不确定我是否理解正确。你有一个类似这样的for循环

for (int i = 0; i < 10; i++)
{
    // do something
    if (!b1)
      i++

    // do something
}
for(int i=0;i<10;i++)
{
//做点什么
如果(!b1)
我++
//做点什么
}
编辑:

如果您使用continue,它只会增加i一次。如果您在循环中使用i++,它显然会增加两次。如果您只想在一个条件下执行,请像这样使用for循环


for(int i=0;i<10)//这与while循环非常相似。

从示例代码中,我认为您正在搜索名称“Alan”。 这是正确的吗

如果是这样,请按如下方式构造循环:

for (int i = 0; i < Collection.Count; i++)
{
    if (Collection[i].Name == "Alan")
    {
        break;  // We found the name we wanted!
    }

    // Otherwise: Keep going to look for the name further on.
}

if (i == Collection.Count)
{
    Console.WriteLine("Alan is not found");
}
else
{
    Console.WriteLine("Alan found at position {0}", i);
}
for(int i=0;i
示例代码将是一个很大的帮助。我不知道“if语句计算为false”是什么意思,如果发生这种情况,那么内部的内容将不会执行,循环将正常继续。感谢您的解释。我知道Where()方法,但我认为这样做是可行的,但我想这不是正确的方法!谢谢。
for (int i = 0; i < 10; i++)
{
    // do something
    if (!b1)
      i++

    // do something
}
for (int i = 0; i < Collection.Count; i++)
{
    if (Collection[i].Name == "Alan")
    {
        break;  // We found the name we wanted!
    }

    // Otherwise: Keep going to look for the name further on.
}

if (i == Collection.Count)
{
    Console.WriteLine("Alan is not found");
}
else
{
    Console.WriteLine("Alan found at position {0}", i);
}