while语句怎么用,C语言while语句用法
一、语句格式:
while(条件表达式) 语句1;
while(条件表达式) { 语句1; 语句2; 语句3; 语句…; }
二、与for循环的比较:
三、应用例题:
#include<iostream>using namespace std;int main(){ int s=0,i=0; while(s<=5050) { i++; s+=i; } cout<<i<<endl; return 0;}
#include<iostream>using namespace std;int main(){ int num,ans=0; cin>>num; while(num!=0) { ans=ans*10+num; num/=10; } cout<<ans; return 0;}
#include<iostream>using namespace std;int main(){ int m=18,n=12,r;//r储存m%n r=m%n; while(r!=0) { m=n; n=r; r=m%n; } cout<<n; return 0;}
#include<iostream>using namespace std;int main(){ int m,n,t,ans,b=1; cin>>m>>n;//输入两个数 if(m<n)//保证最大的数储存在m中 { t=m; m=n; n=t; } ans=m;//枚举法求最小公倍数 while(ans%n!=0) { b++; ans=b*m; } cout<<ans; return 0;}
#include<iostream>using namespace std;int main(){ int time=0,a=1,b=1,c=1,hear=1; bool flag; while(c<=10) { flag=0; time++; if(a<=10) { a++; flag=1; } if(b<=10&&time%2==0) { b++; flag=1; } if(c<=10&&time%4==0) { c++; flag=1; } if(flag) hear++; } cout<<"一共可以听到"<<hear<<"次拍手声。"<<endl; return 0;}
四、课后练习:
本文地址:百科问答频道 https://www.neebe.cn/wenda/934088.html,易企推百科一个免费的知识分享平台,本站部分文章来网络分享,本着互联网分享的精神,如有涉及到您的权益,请联系我们删除,谢谢!