.Net基础篇_学习笔记_第五天_流程控制do-while循环

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

namespace 第六天_do_while循环
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("老师我唱的您满意吗?");
            string answer = Console.ReadLine();
            while (answer == "no")
            {
                Console.WriteLine("老师我再唱一遍,您满意了吗?");
                answer = Console.ReadLine();
            }
            Console.ReadKey();
        }
    }
}

 遇见这种首先执行一边循环体,拿着执行结果然后再去判断是否执行循环,这样的循环,推荐使用do-while循环。

特点:

do-while循环:程序会先执行do中的循环体,执行完后,再去判断do-while循环的循环条件,如果成立,继续执行do中的循环体,如果不成立,则跳出do-while循环。(最少执行一遍循环体,侧重于先做一遍,再执行)。

while循环:先判断再执行,可能一遍也进行循环。

 1 namespace 第六天_do_while循环
 2 {
 3     class Program
 4     {
 5        
 6         static void Main(string[] args)
 7         {
 8             string answer = "";
 9             do
10             {
11                 Console.WriteLine("老师,我唱的您满意吗?yes/no");
12                 answer = Console.ReadLine();
13 
14             }while (answer=="no");
15             Console.WriteLine("OK,可以放学回家了");
16             Console.ReadKey();
17         }
18     }
19 }

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 第六天_do_while循环
 8 {
 9     class Program
10     {
11        
12         static void Main(string[] args)
13         {
14             string name = "";
15             while (name!="q")
16             {
17                 Console.WriteLine("请输入正确的姓名:");
18             }
19             Console.ReadKey();
20         }
21     }
22 }

转成do-while循环则为:

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

namespace 第六天_do_while循环
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = "";
            do
            {
                Console.WriteLine("请输入姓名:");
                name=Console.ReadLine();
            } while (name!="q");
        }
    }
}

 

posted @ 2017-07-20 09:25  MR_L先生  阅读(305)  评论(0编辑  收藏  举报