.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");
        }
    }
}

 

转载于:https://www.cnblogs.com/NBOWeb/p/7209384.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值