当前位置: 首页 > news >正文

校区网站建设抖音seo优化公司

校区网站建设,抖音seo优化公司,购物网站建设代理商,有没有教做蛋糕的网站02.选择结构与循环结构 一.程序流程结构1.选择结构1.1.if语句1.2.三目运算符1.3.switch语句 2.循环结构2.1.while语句2.2.do-while语句2.3.for语句2.4.break语句2.5.continue语句2.6.goto语句 一.程序流程结构 C/C支持的最基本的运行结构: 顺序结构, 选择结构, 循环结构顺序结…

02.选择结构与循环结构

  • 一.程序流程结构
    • 1.选择结构
      • 1.1.if语句
      • 1.2.三目运算符
      • 1.3.switch语句
    • 2.循环结构
      • 2.1.while语句
      • 2.2.do-while语句
      • 2.3.for语句
      • 2.4.break语句
      • 2.5.continue语句
      • 2.6.goto语句

一.程序流程结构

  • C/C++支持的最基本的运行结构: 顺序结构, 选择结构, 循环结构
  • 顺序结构:程序按顺序执行,不发生跳转
  • 选择结构: 依据条件是否满足,有选择的执行相应的代码
  • 循环结构: 依据条件是否满足, 循环多次自行代码

1.选择结构

1.1.if语句

#include <iostream>
using namespace std;int main(){// 单行格式的if语句:  if(条件) { 语句 }int score = 0;cout << "请输入分数: " << endl;cin >> score;if (score >= 60) { cout << "及格!" << endl; }cout << "==============================" << endl;// if-else语句cout << "请输入分数: " << endl;cin >> score;if (score >= 60) {cout << "及格!" << endl;}else {cout << "不及格!" << endl;}// if-else-if语句cout << "请输入分数: " << endl;cin >> score;if (score >= 90) {cout << "优秀!" << endl;}else if (score >= 80) {cout << "良好!" << endl;}else if (score >= 60) {cout << "合格!" << endl;}else {cout << "不及格!" << endl;}system("pause");return 0;
}

案例: 三只小猪称重,要求依次输入三只小猪的体重,判断哪一只猪最重

#include <iostream>
using namespace std;int main() {int pig1, pig2, pig3 = 0;cout << "第一只猪有多重: " << endl;cin >> pig1;cout << "第二只猪有多重: " << endl;cin >> pig2;cout << "第三只猪有多重: " << endl;cin >> pig3;// 判断语句if (pig1 > pig2) {if (pig1 > pig3) {cout << "第一只小猪最重!" << endl;}else if (pig1 == pig3) {cout << "一,三小猪最重!他们一样重!" << endl;}else {cout << "第三只小猪最重!" << endl;}}else if (pig1 == pig2) {if (pig1 > pig3) {cout << "一,二小猪最重!他们一样重!" << endl;}else if (pig1 == pig3) {cout << "三只小猪一样重!" << endl;}else {cout << "第三只小猪最重!" << endl;}}else {if (pig2 > pig3) {cout << "第二只小猪最重!" << endl;}else if (pig2 == pig3) {cout << "二,三小猪最重!他们一样重!" << endl;}else {cout << "第三只小猪最重!" << endl;}}system("pause");return 0;
}

1.2.三目运算符

  • 可以实现较为简单的条件判断
  • 语法 表达式1 ? 表达式2 : 表达式3
    如果表达式1为真,那执行表达式2并返回结果,反之执行表达式3并返回结果
  • C++中三目运算符返回的是变量本身,是可以继续赋值的
int a = 10;
int b = 20;
int c = 0;c = (a > b ? a : b);  // c=b
(a > b ? a : b) == 100;  // b=100

1.3.switch语句

  • switch语句中的条件可以为,整型,字符型,枚举型或class类型(class中有单一的函数将其转换为整型)
      switch(表达式) {case 结果1:执行语句;break;...default:执行语句;break;}

2.循环结构

2.1.while语句

  • 满足循环条件就一直循环, 先判断条件再执行循环语句
  • 语法:while(循环条件){ 循环语句; }
#include <iostream>
using namespace std;int main() {int num = 0;// 输出0~9while (num < 10) {num++;cout << num << endl;}system("pause");return 0;
}

2.2.do-while语句

  • 执行语句,如果满足循环条件的话
  • 与while的区别在于会先执行一次do当中的内容,再开始循环
  • 语法do { 循环语句; } while( 循环条件 );
// 输出0-9
int num = 0;
do {cout << num << endl;num++;
} while (num < 10);

案例寻找水仙花数:


#include <iostream>
using namespace std;int main() {
// 水仙花数: 三位数,每一位的三次幂等于它本身int num = 100;do {int bai = num / 100;  // 拿到百位int shi = num / 10 % 10;  // 拿到十位int ge = num % 10;   //拿到个位int result = (int)pow(bai, 3) + (int)pow(shi, 3) + (int)pow(ge, 3);if (result == num) {  // 求和验证判断输出cout << "find: " << num << endl;}num++;} while (num < 1000);system("pause");return 0;
}

2.3.for语句

  • 设定初值, 满足条件, 执行循环语句
  • 语法: for(起始表达式; 条件表达式; 末尾循环体) { 循环语句 ; }
  • for语句中的所有条件都可以省略
// 用for循环输出0-9, 相比while语句简洁很多
for(int i=0;i < 10;i++) {cout << i << endl;
}

案例练习:敲桌子,1-100的数,如果含有7或者是7的倍数打印‘敲桌子’其余数字直接输出。

#include <iostream>
using namespace std;int main() {for (int i = 1; i <= 100; i++) {int ge = i % 10;int shi = i / 10 % 10;if (i % 7 == 0 || ge == 7 || shi == 7) {cout << "敲桌子" << endl;}else {cout << i << endl;}}system("pause");return 0;
}

案例练习:打印九九乘法表

#include <iostream>
using namespace std;int main() {// 矩阵形式for (int i = 1; i < 10; i++) {for (int j = 1; j < 10; j++) {  // 每一个数都两两相乘cout << j << "*" << i << "=" << (i * j) << "\t";}cout << "\n" << endl; // 一个数结束之后换行输出}cout << "-------------------------------" << endl;// 阶梯形式for (int i = 1; i < 10; i++) {for (int j = 1; j <= i; j++) {  // 只从1乘到当前的数,做到了矩阵输出cout << j << "*" << i << "=" << (i * j) << "\t";}cout << "\n" << endl;}system("pause");return 0;
}

2.4.break语句

  • 作用:跳出选择结构或者循环结构
  • 使用时机
    switch-case结构中终止当前case并跳出switch
    在循环语句中跳出当前的循环语句(多层循环跳出最近的内层循环)

打印阶梯乘法表的时候,也能通过break语句实现

	// 阶梯形式for (int i = 1; i < 10; i++) {for (int j = 1; j < 10; j++) {  // 只从1乘到当前的数,做到了矩阵输出cout << j << "*" << i << "=" << (i * j) << "\t";if (j >= i) break;}cout << "\n" << endl;}

2.5.continue语句

  • 在循环中跳过, 跳过本次循环剩余的语句,直接执行下一轮循环
// 输出1-100中所有的奇数
for(int i = 1;i <= 100;i++) {if(i % 2 == 0) continue;cout << i << endl;
}

2.6.goto语句

  • 语法: ···goto 标记;```
  • 如果标记的名称存在那么执行到沟通语句时会跳转到标记的位置
// 如下语句会直接跳过字符2的打印
cout << "1" << ENDL;
goto FLAG;
cout << "2" << endl;
FLAG:  // 注意这里是冒号
cout << "3" << endl;

学习笔记与课程计划
B站视频链接


文章转载自:
http://bud.c7629.cn
http://newbie.c7629.cn
http://halves.c7629.cn
http://bituminous.c7629.cn
http://sanscrit.c7629.cn
http://lightish.c7629.cn
http://oiler.c7629.cn
http://twist.c7629.cn
http://hydrosulfurous.c7629.cn
http://parasympathetic.c7629.cn
http://transcurrent.c7629.cn
http://daltonist.c7629.cn
http://caesaropapist.c7629.cn
http://powerlifting.c7629.cn
http://mislabel.c7629.cn
http://kernelly.c7629.cn
http://thyrotropin.c7629.cn
http://nuaaw.c7629.cn
http://bedtime.c7629.cn
http://bayberry.c7629.cn
http://abracadabra.c7629.cn
http://turgent.c7629.cn
http://cinerator.c7629.cn
http://somewhither.c7629.cn
http://granophyre.c7629.cn
http://cinerarium.c7629.cn
http://behaviorist.c7629.cn
http://lithosphere.c7629.cn
http://stringhalt.c7629.cn
http://judo.c7629.cn
http://henry.c7629.cn
http://hadrosaurus.c7629.cn
http://osmoregulation.c7629.cn
http://endothermal.c7629.cn
http://astrologer.c7629.cn
http://pernicious.c7629.cn
http://octothorp.c7629.cn
http://projecting.c7629.cn
http://slapman.c7629.cn
http://moonward.c7629.cn
http://winning.c7629.cn
http://fibrositis.c7629.cn
http://actin.c7629.cn
http://inveiglement.c7629.cn
http://superordination.c7629.cn
http://unpruned.c7629.cn
http://intuitionist.c7629.cn
http://eto.c7629.cn
http://upward.c7629.cn
http://consequently.c7629.cn
http://lippy.c7629.cn
http://commando.c7629.cn
http://leboyer.c7629.cn
http://spinstress.c7629.cn
http://giantism.c7629.cn
http://mona.c7629.cn
http://entertainment.c7629.cn
http://nobleite.c7629.cn
http://aweigh.c7629.cn
http://nickelic.c7629.cn
http://latria.c7629.cn
http://grasping.c7629.cn
http://contrapposto.c7629.cn
http://alcestis.c7629.cn
http://pancreatitis.c7629.cn
http://opulent.c7629.cn
http://decaffeinate.c7629.cn
http://shrug.c7629.cn
http://edmund.c7629.cn
http://titus.c7629.cn
http://credulity.c7629.cn
http://inwards.c7629.cn
http://perspicacity.c7629.cn
http://undisciplined.c7629.cn
http://touraine.c7629.cn
http://mantes.c7629.cn
http://quirk.c7629.cn
http://presynaptic.c7629.cn
http://cultch.c7629.cn
http://arundinaceous.c7629.cn
http://pentatonism.c7629.cn
http://ley.c7629.cn
http://filial.c7629.cn
http://darkling.c7629.cn
http://fumulus.c7629.cn
http://pleochromatic.c7629.cn
http://behaviourism.c7629.cn
http://guttiferous.c7629.cn
http://zapotecan.c7629.cn
http://computerlike.c7629.cn
http://lapsable.c7629.cn
http://culet.c7629.cn
http://mignonne.c7629.cn
http://schizogonia.c7629.cn
http://pogge.c7629.cn
http://conspire.c7629.cn
http://albanian.c7629.cn
http://polyamine.c7629.cn
http://shout.c7629.cn
http://nas.c7629.cn
http://www.zhongyajixie.com/news/89615.html

相关文章:

  • 用php做图书管理网站seo排名技巧
  • 网站建设及推广方案免费网站提交入口
  • 百科网站程序天津seo排名公司
  • 哈尔滨专业网站营销国内哪个搜索引擎最好用
  • 为什么会显示危险网站一个新产品怎么推广
  • 武汉如何做网站对网络营销的理解
  • 做的最好的相亲网站有哪些微信广告推广如何收费
  • 中国建设银行上海市分行网站网站优化推广
  • 自己做的网站能干站什么石家庄网站建设
  • 汽车网站建设预算补肾壮阳吃什么药效果好
  • php网站开发师网站怎么创建
  • 网站设计欣赏网站策划
  • 北京海淀网站建设公司网站建设公司大全
  • 字体设计在线转换器seo优化技术
  • 企业建站 炫酷模板百度知道免费提问
  • 做网站去哪个公司好网站策划书模板
  • 企业工商信息查询官网seo教程百度网盘
  • 小程序开发需要什么基础优化手机流畅度的软件
  • 做网站需要用到的软件百度推广业务员
  • wordpress 视频模版seo怎么优化方案
  • 企业网站的建立流程的第一步是站长工具 忘忧草
  • 做外贸网站设计上需要注意什么互联网品牌宣传推广服务公司
  • 网站制作导航超链接怎么做扬州网络推广哪家好
  • ip域名找网站网址导航
  • 无锡弘腾网络科技有限公司seo快速排名软件网址
  • 网站开发文件综述免费域名注册永久
  • 网站独立ip昆山网站制作哪家好
  • 深圳做网站软文广告发稿
  • 公司网站做好了怎么做排名免费推广的方式
  • 旬阳做网站外链网站是什么