第十五节 流程控制 五

循环结构(while语句)

while(循环条件)

{

//循环体

}

计算1+2+3...+100的和。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HE
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 1;
            int sum=0;
            while (i<= 100)
            {
                sum = sum + i;
                i = i + 1;
            }
            Console.WriteLine("1+2+3...+100="+sum);
        }
    }
}

  死循环

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HE
{
    class Program
    {
        static void Main(string[] args)
        {
            
            while (100<200)
            {
                Console.WriteLine("出现死循环");
            }
            
        }
    }
}

  

计算2+4+6+...100的和。用while循环做。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HE
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 2;
            int sum=0;
            while (i<= 100)
            {
                sum = sum + i;
                i = i + 2;
            }
            Console.WriteLine("2+4+6+8...+100="+sum);
        }
    }
}

  

posted on 2013-04-29 19:11  杨柳清枫2012  阅读(118)  评论(0)    收藏  举报

导航