码迷,mamicode.com
首页 > Windows程序 > 详细

C#for(;;)是什么意思?

时间:2018-04-23 11:06:15      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:返回   sys   .text   内容   跳出循环   括号   AC   ati   ram   

一,正常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,第二个分号前的内容是每次执行前都要判断的(如果该处表达式的值为真,那么执行循环体,如果为假,那么就跳出循环体)

 

C#for(;;)是什么意思?

标签:返回   sys   .text   内容   跳出循环   括号   AC   ati   ram   

原文地址:https://www.cnblogs.com/May-day/p/8892599.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!