4-8.goto文と深層ループ

7573 ワード

  • goto文作用
  • 深層ループ(ネストされたループ)から飛び出す
  • 実装サイクル(アセンブリの実装と同様)
  • 深層サイクルから飛び出して
  • を実現する.
    #include
    using namespace std;
    int main(int argc, char* argv[])
    {	//    goto  ,            
    	/*
    	    :
    			1  2  3  4
    			5  6  7  8
    			9 10 11 12
    	       1,2,5,6,9,10,
    	*/
    	/*************************    *************************/
    	int intArray[3][4] = { {1,2,3,4},{5,6,7,8},{9,10,11,12} };
    	//        、    
    	int colSize = sizeof(intArray[0]) / sizeof(int);
    	int rowSize = sizeof(intArray) / sizeof(int) / colSize;
    	cout << " :"<<colSize << endl;
    	cout << " :" << rowSize << endl;
    	//        
    	for (int i = 0; i < rowSize; i++)
    	{
    		//        
    		for (int j = 0; j < colSize; j++) 
    		{
    			if (j == 2)
    			{
    				goto label;
    				//  break     ,  break        ,goto        
    				//label      ,    ,             
    			}
    			cout << intArray[i][j] <<" ";
    		}
    		label://label  
    		cout << endl ;
    	}
    	return 0;
    }
    
  • 実装サイクル(一般的には推奨されない)
  • アセンブリでループを実行する場合は、ジャンプ文のみ使用できます.
    次にgoto文を用いてループを実現する
    	int i = 0;
    loop:
    	cout << i << endl;
    	i++;
    	if (i < 10) 
    	{
    		goto loop;
    	}
    
  • VS 2015でプログラムのアセンブリ言語
  • を表示する方法
    クリックしてください