【问题标题】:Nested for/if loop and statement [closed]嵌套 for/if 循环和语句 [关闭]
【发布时间】:2017-08-25 09:33:42
【问题描述】:
if (first statement) 
{
    for (first loop) 
    {
        if (second statement) 
        {
            for (second loop) 
            {       
                if (third statement) 
                {
                    Do something;
                }
            } 
        }
    }
}

问题是,如果第三个语句是 false ,它会进入第一个循环还是第二个循环? c#

【问题讨论】:

  • 试一试,你会看到会发生什么。
  • 测试时会发生什么?
  • 当然是先第二个循环,最后第一个循环结束。
  • 是的,我在绘制块图时得到了它

标签: c# loops for-loop if-statement nested


【解决方案1】:

如果第三个 if 为假,它将在完成第二个循环之后完成它所属的 for 循环,直到 if 为真或完成并返回第一个循环

【讨论】:

    【解决方案2】:

    如果第三个语句为假,您只需继续您的第二个for 循环,因为它只是此for 循环的一次迭代。您将留在其中,直到不再验证使您处于 for 循环中的条件。然后你会回到你的第一个 for 循环以同样的理由。

    【讨论】:

      【解决方案3】:
      if (first statement) {
      for (first loop) {
      if (second statement) {
      for (second loop) {
      if (third statement) { // if false, will still do what comes after here
      Do something;
      }
      if{ 4th statement // will still do this.
      }
      } 
      }
      }
      }
      

      仍然会循环

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-23
        • 1970-01-01
        相关资源
        最近更新 更多