大家都知道,在bat类型程序中,goto是一种循环。那么,在c++中,如何使用goto呢?
定义
goto是c++中的关键字,使程序跳到你的标签位置。
使用
与bat不同的是,在c++里,你必须先定义它。
例:
#include<bits/stdc++.h>
using namespace std;
int a,b;
int main(){
cin>>a>>b;
goto l;
l:
cout<<a+b;
goto l;
return 0;
}
如何跳出循环?
可以定义一个if语句。
#include<bits/stdc++.h>
using namespace std;
int a,b;
int main(){
cin>>a>>b;
int m=a+b;
goto l;
l:
cout<<m<<endl;
m--;
if(m>=0) goto l;
return 0;
}