zoukankan      html  css  js  c++  java
  • .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");
            }
        }
    }
  • 相关阅读:
    Java编程的逻辑 (62)
    float示例
    如何避免在短时间内按钮被多次重复点击
    前端(jQuery)(9)-- jQuery菜单
    前端(jQuery)(8)-- jQuery元素遍历
    前端(jQuery)(6)-- jQuery的扩展与noConflict
    前端(jQuery)(5)-- jQuery AJAX异步访问和加载片段
    xampp中tomcat服务器无法启动
    前端(jQuery)(4)-- jQuery隐藏显示与淡入淡出、滑动、回调
    自定义事件总结
  • 原文地址:https://www.cnblogs.com/NBOWeb/p/7209384.html
Copyright © 2011-2022 走看看