隐藏

C#如何跳出if语句以及程序运行时间

发布:2023/6/9 17:50:33作者:管理员 来源:本站 浏览次数:538

切正题前,using System.Diagnostics;


Stopwatch.start(),stop(),restart(),,stopwatch.elapsedticks可以查询程序的运行时间。


好了,跳出if语句有两种方式,return和goto;


方法A中,不存在跳转,时间为105,方法B中,用了goto,时间为7;


说到这里,就得提到一个程序优化的原则:


多层嵌套条件语句的时候,如果知道结果,if玩了一条就直接返回,或者说跳出,这样更高效。A中是完全执行了一千遍,且每个if都运行了。


关于goto:我就知道要慎用。。。


关于return:直接跳出方法体,而不是if语句。


    public static void MethodA()

           {

               int a = 0;

               int count = 0;

               Stopwatch stopwatch = new Stopwatch();

               stopwatch.Start();

               for(int i=0;i<1000;i++)

               {

                   if (a == 0)

                   {

                       count += 1;

                   }

                   if (a == 1)

                   {

                       count += 1;

                   }

                   if (a == 2)

                   {

                       count += 1;

                   }

                   if (a == 3)

                   {

                       count += 1;

                   }

               }

               stopwatch.Stop();

               Console.WriteLine(stopwatch.ElapsedTicks);

           }

           public static void MethodB()

           {

               int a = 0;

               int count = 0;

               Stopwatch stopwatch = new Stopwatch();

               stopwatch.Start();

               for (int i = 0; i < 1000; i++)

               {

                   if (a == 0)

                   {

                       count += 1;

                       goto next;

                   }

                   if (a == 1)

                   {

                       count += 1;

                   }

                   if (a == 2)

                   {

                       count += 1;

                   }

                   if (a == 3)

                   {

                       count += 1;

                   }

               }

               next: stopwatch.Stop();

               Console.WriteLine(stopwatch.ElapsedTicks);

           }


再来一段新写的,好好体会一下.意义嘛没什么意思,就是知道了return,就可以跳出方法体了。


   public bool IsCollinear(int a,int b)

   {

     if((a-b)>0)

     {

       if((a-b)==0)

       return false;

     }

     else if((a-b)<0)

     {

       return true;

     }

   }


goto 语句用于直接在一个程序中转到程序中的标签指定的位置,标签实际上由标识符加上冒号构成。


语法形式如下。


goto Labell;

   语句块 1;

Labell

   语句块 2;

如果要跳转到某一个标签指定的位置,直接使用 goto 加标签名即可。


在上面的语句中使用了 goto 语句后,语句的执行顺序发生了变化,即先执行语句块 2,再执行语句块 1。


此外,需要注意的是 goto 语句不能跳转到循环语句中,也不能跳出类的范围。


由于 goto 语句不便于程序的理解,因此 goto 语句并不常用。


【实例】使用 goto 语句判断输入的用户名和密码是否正确,如果错误次数超过3次,则输出“用户名或密码错误次数过多!退出!”。


根据题目要求,假设用户名为 aaa、密码为 123,代码如下。


   class Program

   {

       static void Main(string[] args)

       {

           int count = 1;

       login:

           Console.WriteLine("请输入用户名");

           string username = Console.ReadLine();

           Console.WriteLine("请输入密码");

           string userpwd = Console.ReadLine();

           if (username == "aaa" && userpwd == "123")

           {

               Console.WriteLine("登录成功");

           }

           else

           {

               count++;

               if (count > 3)

               {

                   Console.WriteLine("用户名或密码错误次数过多!退出!");

               }

               else

               {

                   Console.WriteLine("用户名或密码错误");

                   goto login;//返回login标签处重新输入用户名密码

               }

           }

       }

   }


执行上面的代码,效果如下图所示。