我是靠谱客的博主 傲娇镜子,最近开发中收集的这篇文章主要介绍代码执行顺序 continue、break、goto、return。,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

正常计算机代码的执行顺序是从上至下,除非有逻辑语句跳转。

以前没了解goto的使用,现在研究一下goto还是挺有意思的。

下面是c++简单的代码demo。

void Main::xecutionTest()
{
     for (int i = 0; i<10; i++)
     {
          switch (getCode())
          {
              case 0:
                   cout << "Ignore this item";
                   continue;
              case 1:
              mCase:
                   cout << "End the whole cycle";
                   break;
              case 2:
                   cout << "Jump to the specified position";
                   goto mCase;
              case 3:
                   cout << "End this function";
                   return;
              default:
                   cout <<"Invalid selection";
                   break;
           }
     }
     cout << "Normal execution";
}

 

下面是c#简单的代码(c++也适用c#)demo。

       

         void xecutionTest()
        {
            for (int i = 0; i < 10; i++)
            {
                switch (getCode())
                {
                    case 0:
                        Console.WriteLine("Ignore this item");
                        continue;
                    case 1:
                        Console.WriteLine("End the whole cycle");
                        break;
                    case 2:
                        Console.WriteLine("Jump to the specified position");
                        goto case 1;
                    case 3:
                        Console.WriteLine("End this function");
                        return;
                    default:
                        Console.WriteLine("Invalid selection");
                        break;
                }
            }
            Console.WriteLine("Normal execution");
        }


  下面是java简单的代码demo 。

 public static void main(String[] args) {
      for (int i = 0; i<10; i++)
      {
           switch (getCode())
           {
               case 0:
                System.out.println("Ignore this item");
                    continue;
               case 1:
               mCase:
                System.out.println("End the whole cycle");
                    break;
               case 2:
                System.out.println("Jump to the specified position");
                break mCase;
               case 3:
                System.out.println("End this function");
                    return;
               default:
                System.out.println("Invalid selection");
                    break;
            }
      }
      System.out.println("Normal execution");
 }

 

下面是php简单的代码demo 。

function xecutionTest(){

 for (int i = 0; i<10; i++)
 {
     switch (getCode())
     {
         case 0:
            echo "Ignore this item";
            continue;
         case 1:
            mCase:
            echo "End the whole cycle";
            break;
         case 2:
            echo "Jump to the specified position";
            goto mCase;
         case 3:
            echo "End this function";
            return;
         default:
            echo "Invalid selection";
            break;
         }
     }
     echo "Normal execution";
 }
}









最后

以上就是傲娇镜子为你收集整理的代码执行顺序 continue、break、goto、return。的全部内容,希望文章能够帮你解决代码执行顺序 continue、break、goto、return。所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(66)

评论列表共有 0 条评论

立即
投稿
返回
顶部