2-28
(1)if...else
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <iostream> using namespace std; int main() { while (true) { char a; cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:" <<endl; cin>>a; if (a=='A') {cout<<"Date has been added"<<endl; continue;} else if (a=='D') {cout<<"Date has been deleted"<<endl; continue;} else if (a=='S') {cout<<"Date has been sorted"<<endl; continue;} else if (a=='Q') break; else {cout<<"Input error"<<endl; continue;} } return 0; }
(2)switch
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <iostream> using namespace std; int main() { while (true) { char c; cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:"<<endl; cin>>c; switch (c){ case('A'):cout<<"Date has been added"<<endl; continue; case('D'):cout<<"Date has been deleteed"<<endl; continue; case('S'):cout<<"Date has been sorted"<<endl; continue; case('Q'):break; default:cout<<"Input error"<<endl; continue; }break; } return 0; }
2-29
(1)while
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <iostream> #include <cmath> using namespace std; int main() { int n=2,i,k,a=0; while(n<=100){ k=sqrt(n); for(i=2;i<=k;i++) if(n%i==0) break; if(i>k){ a++; cout<<n; cout<<" ";} if(a%5==0) cout<<endl; n++; } return 0; }
(2)do while
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <iostream> #include <cmath> using namespace std; int main() { int n=2,i,k,a=0; do{ k=sqrt(n); for(i=2;i<=k;i++) if(n%i==0) break; if(i>k){ a++; cout<<n; cout<<" ";} if(a%5==0) cout<<endl; n++; }while(n<=100); return 0; }
(3)for
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <iostream> #include <cmath> using namespace std; int main() { int n,i,k,a=0; for(n=2;n<=100;n++){ k=sqrt(n); for(i=2;i<=k;i++) if(n%i==0) break; if(i>k){ a++; cout<<n; cout<<" ";} if(a%5==0) cout<<endl; } return 0; }
2-32
(1)while
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <iostream> using namespace std; int main(){ int a=41,b; cout<<"Please guess a number."<<endl; cin>>b; while(a!=b){ while(a>b){ cout<<"The number is bigger than yours."<<endl; cin>>b;} while(a<b){ cout<<"The number is smaller than yours."<<endl; cin>>b;} } cout<<"You guessed it."<<endl; return 0; }
(2)do...while
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <iostream> using namespace std; int main(){ int a=41,b; cout<<"Please guess a number."<<endl; cin>>b; do{ if(a>b){ cout<<"The number is bigger than yours."<<endl; cin>>b;} if(a<b){ cout<<"The number is smaller than yours."<<endl; cin>>b;} }while(a!=b); cout<<"You guessed it."<<endl; return 0; }
2-34
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <iostream> using namespace std; int main(){ int i,j,k,n=0; for(i=1;i<=5;i++) {for(j=i;j<=5;j++) if(i!=j) {for(k=j+1;k<=5;k++) if(k!=i&&k!=j){ n++; cout<<i<<","<<j<<","<<k<<endl;} } } cout<<"一共有"<<n<<"种取法"<<endl; return 0; }
https://www.cnblogs.com/libing-072921/p/10536335.html
https://www.cnblogs.com/Yyaoyyy/p/10547742.html
https://www.cnblogs.com/jzgjzg/p/10555540.html