goto 是 c++ 的关键字之一,语法为
goto [lable]
其中[lable] 为标签,用[name]:; 来定义
话不多说,直接上代码
#include<bits/stdc++.h>
using namespace std;
int main(){
int a=1;
this_is_a_lable:;//定义标签
cout<<a++<<" ";
if(a<=4) goto this_is_a_lable;
return 0;
}这段代码会输出1 2 3 4
可能现在好处不明显,但巧妙的用,就能使代码明了
顺便提一下, 也可以跳至后面,如下:
#include<bits/stdc++.h>
using namespace std;
int main(){
int a;
cin>>a;
if(a>4) goto _1;
cout<<"a不大于4时,输出这段"<<endl;
_1:;
cout<<"无论如何,都会输出这段";
return 0;
}