zoukankan      html  css  js  c++  java
  • C#for(;;)是什么意思?

    一,正常for循环我们都接触过很多,如下,我们都理解

               int[] tt = {1,2,3,4,5,6 };
                for (int i = 1; i < 6; i++)
                {
                    Console.WriteLine(tt[i]);
                }    

    二,但是for(;;)实际上它的含义是什么呢?

    含义: for后的圆括号中,第一个分号前的内容是执行第一次循环前执行的,第二个分号前的内容是每次执行前都要判断的(如果该处表达式的值为真,那么执行循环体,如果为假,那么就跳出循环体)

    三,是不是觉得写到这里大家觉得那么常见的都还要介绍?那我们来点扩展,如下代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace W
    {
        class Program
        {
            static void Main(string[] args)
            {
                int t2 = 0;
                int t1 = 0;
                for (Demo.First(ref t1); Demo.Scend(t1, ref  t2); )
                {
                    Console.WriteLine(t2);
                }
            }
    
        }
        public class Demo
        {
            public static int j = 5;
            public static bool First(ref int t1)
            {
                t1 = 1;
                return false;
            }
    
            public static bool Scend(int t1,ref int t2)
            {
                if (j > 0)
                {
                    j = j - t1;
                    t2 = j;
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }

    三,在上述代码中我们看到for (Demo.First(); Demo.Scend(1,ref te); )是在分号调用两个方法,是不是跟平常使用的不一样??那为什么可以这样用呢?我们根据for(;;)的含义来解析。

    1,第一个分号前的内容是执行第一次循环前执行的,而第一个分号不会判断true和false,所以当定义返回false时也不会跳出循环

    2,第二个分号前的内容是每次执行前都要判断的(如果该处表达式的值为真,那么执行循环体,如果为假,那么就跳出循环体)

  • 相关阅读:
    Phonics 自然拼读法 s,a,t,i,p,n Teacher:Lamb
    English Voice of <<City of stars>>
    English trip EM2-LP-1A Hi Teacher:Taylor
    English trip EM2-PE-6B Teacher:Taylor,Patrick(2019.12.2)
    English trip EM2-PE-6A Family Relationship Teacher:Taylor
    keras各种优化方法总结 SGDmomentumnesterov
    keras做DNN
    keras、 tensor flow 教程
    DNN例子
    tensor flow 的两种padding方式
  • 原文地址:https://www.cnblogs.com/May-day/p/8892599.html
Copyright © 2011-2022 走看看