死循环是一种十分有用的结构,今天,老王为大家分享一些C++的死循环方式。
一、while语句
#include<iostream>
using namespace std;
int main(){
while(1){
cout<<"hello world!"<<endl;
return 0;
}
运行结果:
二、goto语句
#include<iostream>
using namespace std;
int main(){
here:
cout<<"hello world!"<<endl;
goto here;
return 0;
}
运行结果:
三、for循环
#include<iostream>
using namespace std;
int main(){
for(int i=0;i >= 0;i++){
cout << "hello world!" << endl;
}
return 0;
}
运行结果:
注意:运行死循环需谨慎,否则你的电脑可能会成这样:
好了,再见!祝大家电脑永远不冒烟😂!