C++ 流程控制(分支、循环、跳转)

编程入门 行业动态 更新时间:2024-10-28 14:37:11

C++ 流程控制(分支、循环、<a href=https://www.elefans.com/category/jswz/34/1769274.html style=跳转)"/>

C++ 流程控制(分支、循环、跳转)

#include<iostream>using namespace std;int main()
{// 单分支和双分支cout << "please enter your age:" << endl;int age;cin >> age;if(age >= 18){cout << "welcome! adult." << endl;}else{cout << "only adult can pass." << endl;}age >= 18 ? cout << "welcome, adult!" << endl : cout << "only adult can pass" << endl;cout << (age >= 18 ? "welcome, adult!" : "only adult can pass!") << endl;//多分支cout << "please enter your age:" << endl;int age;cin >> age;if (age < 12){cout << "child" << endl;}else if (age <= 18){cout << "teenager" << endl;}else if (age <= 35){cout << "keep going" << endl;}else{cout << "enjoy your life" << endl;}// switchcout << "please enter your score level:" << endl;char score;cin >> score;switch (score){case 'A':cout << "score >= 90 && score <= 100" << endl;break;case 'B':cout << "score >= 80 && score < 90" << endl;break;case 'C':cout << "score < 80" << endl;break;default:cout << "wrong score" << endl; break;}// whilecout << "loop strat...\n" << endl;int i = 1;while(i <= 10){cout << "hello world" << i++ << endl;}do whileint i = 10;do{cout << i-- << "次循环" << endl;} while (i > 0);//for循环for (int i = 0; i < 10; i++){cout << i << "次循环" <<endl;}//范围for循环for (int num : {1, 3, 5, 7, 9}){cout << "序列中输出的数据是" << num << endl;}cin.get();cin.get();
}
// continue
// 逢7过for (int num = 0; num < 101; num++){cout << "\t";if (num % 7 == 0 || num % 10 == 7 || num / 10 == 7){continue;}cout << num;}
// goto
int x = 0;cout << "循环开始。。。" << endl;
begin:do{cout << x++ << "次循环" << endl;} while (x <= 10);if (x < 15){cout << "回到原点" << endl;goto begin;}

更多推荐

C++ 流程控制(分支、循环、跳转)

本文发布于:2023-12-03 13:56:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1656113.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:跳转   分支   流程

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!