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

设计网站账号新闻式软文经典案例

设计网站账号,新闻式软文经典案例,做h5网站的公司,免费b2b网站大全黄页文章目录 1. break2. continue 语句3. goto 语句goto的存在 4. 跳出多重循环4.1 goto 直接跳转4.2 C11及其后版本的 return 语句4.3 使用标志变量 在C中,控制语句用于管理程序的执行流程。常见有 break、continue 和 goto。 1. break break语句主要用于在循环或者s…

文章目录

  • 1. break
  • 2. continue 语句
  • 3. goto 语句
    • goto的存在
  • 4. 跳出多重循环
    • 4.1 goto 直接跳转
    • 4.2 C++11及其后版本的 `return` 语句
    • 4.3 使用标志变量

在C++中,控制语句用于管理程序的执行流程。常见有 break、continue 和 goto。

1. break

break语句主要用于在循环或者switch语句中中断当前执行的流程,并跳出循环或switch结构。

用法:

  • 在switch语句中,从一个case分支中直接跳出 switch,避免执行后续的case分支
  • 在循环结构中,立即结束循环,跳出当前所在的循环体。也就是说在一个双重循环(或者更多层嵌套的循环)中使用break,它只能终止内层的循环,而不会终止外层的循环。

例如,跳出 switch 结构

int num = 2;
switch (num) {case 1:cout << "Number is 1" << endl;break;  // 跳出switchcase 2:cout << "Number is 2" << endl;break;  // 跳出switchcase 3:cout << "Number is 3" << endl;break;  // 跳出switch        default:cout << "Number is not 1, 2 or 3" << endl;
}

例如,跳出当前循环体

#include <iostream>
using namespace std;
int main()
{int i;for (i = 1; i <= 10; i++) {if (i == 5) {break;  //i等于5时,break终止循环,跳出for循环}cout << i << " ";}return 0;
}
// 控制台输出内容为 1 2 3 4

再如,双重循环,代码如下所示。在这个例子中,当 j == 3 时,break语句会终止内层的for循环,但外层的for循环(即 i 的循环)会继续执行

#include <iostream>
using namespace std;
int main() {for (int i = 0; i < 5; i++) {for (int j = 0; j < 5; j++) {if (j == 3) {break;  // 只能跳出内层的for循环}cout << "i = " << i << ", j = " << j << endl;}}return 0;
}

在这里插入图片描述

2. continue 语句

跳过当前这次循环的剩余代码,立即开始下一次循环,continue不会终止循环
例如:

#include <iostream>
using namespace std;
int main()
{int i;for (i = 1; i <= 10; i++) {if (i == 5) {continue;  // 当i等于5时提前结束本次循环,开始下一次循环}cout << i << " ";}return 0;
}
// 控制台输出为 1 2 3 4 6 7 8 9 10

3. goto 语句

  • 无条件地从一个地方跳到程序中的另一个任意位置,会降低代码的可读性和可维护性,不建议使用
  • 定义一个标签(label),然后通过goto语句跳转到该标签处执行代码
  • 标签以冒号:结尾,并放在代码的某一行上
    例如:
#include <iostream>
using namespace std;int main()
{int n = 0;
loop_start:cout << "请输入一个数字(输入 0 退出):";cin >> n;if (n != 0){cout << "你输入的是:" << n << endl;goto loop_start;}cout << "GoodBye!" << endl;return 0;
}

在这里插入图片描述

goto的存在

尽管不推荐goto语句,但它仍然存在于C++语言中。历史原因,C++来源于C,C从汇编和早期的编程语言中演化而来。早期语言中,goto是实现控制流的主要方式之一。在某些旧的代码库或者嵌入式设备中的固件中,可能大量使用了goto语句,为了兼容性保留,能够维护和扩展,不必重写大量逻辑。

也可以用于跳出嵌套较深的情况。见后面的4. 跳出多重循环

goto在某些情况下还可以用于处理资源释放或者异常处理,有时候在一些嵌入式系统或者需要高性能的应用中,直接用goto进行异常处理或者资源清理可能会更加简洁。例如:

#include <iostream>
#include <fstream>
using namespace std;int main() 
{ifstream file;file.open("example.txt");if (!file.is_open()) {goto error;  // 如果文件打开失败,跳到错误处理部分}// 文件操作代码// ...file.close();return 0;error:cout << "Error opening file!" << endl;return 1;
}

在这个例子中,如果文件打开失败,程序会直接跳转到error部分,执行错误处理逻辑。虽然这种方式不如C++中的try-catch异常处理机制规范,但在某些对性能要求极高或资源有限的场合,比如嵌入式开发、驱动程序编写领域等,这种方式提供了一种相对低开销的替代方案,可能会更好。

4. 跳出多重循环

4.1 goto 直接跳转

例如:

#include <iostream>
using namespace std;
int main() 
{bool found = false;for (int i = 0; i < 10; i++) {for (int j = 0; j < 10; j++) {if (i * j == 50) {found = true;goto end_loop;  // 跳出双重循环}}}end_loop:if (found) {cout << "Found the pair!" << endl;} else {cout << "Pair not found!" << endl;}return 0;
}

4.2 C++11及其后版本的 return 语句

可以使用 return 直接结束函数的执行,函数都结束了,从而循环结束了,也就是说自动跳出所有嵌套的循环。例如

#include <iostream>
using namespace std;
void processLoop()
{int i, j;for (i = 0; i < 5; i++){for (j = 0; j < 5; j++){if (j == 3){return ;//直接结束函数体,从而跳出循环}cout << "i = " << i << ", j = " << j << endl;}}cout << "j==3时,提前退出所有循环" << endl;//不会执行,因为return提前结束了函数体   
}
int main()
{processLoop();   //调用函数cout << "循环在 j 为 3 时提前结束" << endl; //控制台输出return 0;
}

在这里插入图片描述
这里,如果 return 语句放在了 main 函数中,在 j == 3 时,则会直接结束整个程序的执行。

与前面放在普通函数里有所不同,普通函数因为return结束了自身的执行,还可以返回到主调函数中。

然而,在main函数中,return 则意味着程序完全终止,即程序立即退出。

#include <iostream>
using namespace std;
int main()
{int i, j;for (i = 0; i < 5; i++){for (j = 0; j < 5; j++){if (j == 3){return 0;}cout << "i = " << i << ", j = " << j << endl;}}return 0;
}

4.3 使用标志变量

用标志变量来标记是否需要跳出所有循环,而不是直接使用 return 结束程序。

#include <iostream>
using namespace std;
int main()
{int i, j;bool flag = false; //起到标志作用的变量for (i = 0; i < 5; i++){for (j = 0; j < 5; j++){if (j == 3){flag = true; //设置标志break; //先跳出内层循环}cout << "i = " << i << ", j = " << j << endl;}if (flag) //判断标志{break; //跳出外层循环}           }cout << "退出了双重循环" << endl;return 0;
}

在这里插入图片描述


文章转载自:
http://bahai.xhqr.cn
http://bierkeller.xhqr.cn
http://keppen.xhqr.cn
http://beravement.xhqr.cn
http://khaph.xhqr.cn
http://microtec.xhqr.cn
http://opac.xhqr.cn
http://kaiserism.xhqr.cn
http://metasomatic.xhqr.cn
http://hebetate.xhqr.cn
http://hawkshaw.xhqr.cn
http://thicket.xhqr.cn
http://artie.xhqr.cn
http://commonness.xhqr.cn
http://bombycid.xhqr.cn
http://celestine.xhqr.cn
http://poco.xhqr.cn
http://alkalization.xhqr.cn
http://unhappily.xhqr.cn
http://sebacic.xhqr.cn
http://membranate.xhqr.cn
http://fiz.xhqr.cn
http://kayak.xhqr.cn
http://unrequited.xhqr.cn
http://entry.xhqr.cn
http://garbologist.xhqr.cn
http://melilla.xhqr.cn
http://portray.xhqr.cn
http://acquired.xhqr.cn
http://anoscope.xhqr.cn
http://acousticon.xhqr.cn
http://underbush.xhqr.cn
http://rotatablely.xhqr.cn
http://tamandua.xhqr.cn
http://middlescent.xhqr.cn
http://emeric.xhqr.cn
http://drinkable.xhqr.cn
http://treasuryship.xhqr.cn
http://pericynthion.xhqr.cn
http://americanise.xhqr.cn
http://aslef.xhqr.cn
http://epigamic.xhqr.cn
http://enflower.xhqr.cn
http://plumbaginous.xhqr.cn
http://abortive.xhqr.cn
http://hadrosaurus.xhqr.cn
http://isometropia.xhqr.cn
http://turbocar.xhqr.cn
http://gemological.xhqr.cn
http://incalescent.xhqr.cn
http://vibrioid.xhqr.cn
http://bosnia.xhqr.cn
http://eliminator.xhqr.cn
http://dialytic.xhqr.cn
http://mira.xhqr.cn
http://immunocyte.xhqr.cn
http://mesmerization.xhqr.cn
http://hydrophily.xhqr.cn
http://custodian.xhqr.cn
http://metatherian.xhqr.cn
http://perithecium.xhqr.cn
http://unbudgeable.xhqr.cn
http://pro.xhqr.cn
http://mudguard.xhqr.cn
http://mesopelagic.xhqr.cn
http://renormalization.xhqr.cn
http://disassimilation.xhqr.cn
http://dottle.xhqr.cn
http://heyduck.xhqr.cn
http://downsize.xhqr.cn
http://hectograph.xhqr.cn
http://peacekeeping.xhqr.cn
http://lateroversion.xhqr.cn
http://shrubbery.xhqr.cn
http://tentless.xhqr.cn
http://spearman.xhqr.cn
http://unitable.xhqr.cn
http://laburnum.xhqr.cn
http://beefeater.xhqr.cn
http://leuco.xhqr.cn
http://refinement.xhqr.cn
http://lxv.xhqr.cn
http://startling.xhqr.cn
http://bacchanalian.xhqr.cn
http://middlebuster.xhqr.cn
http://benne.xhqr.cn
http://calque.xhqr.cn
http://ravelment.xhqr.cn
http://aesc.xhqr.cn
http://bushido.xhqr.cn
http://byplot.xhqr.cn
http://balistraria.xhqr.cn
http://andante.xhqr.cn
http://deerhound.xhqr.cn
http://hairtrigger.xhqr.cn
http://versicle.xhqr.cn
http://multiethnic.xhqr.cn
http://diacidic.xhqr.cn
http://backsaw.xhqr.cn
http://multicoloured.xhqr.cn
http://www.15wanjia.com/news/100106.html

相关文章:

  • 青岛wordpress建站网络营销推广的手段
  • mm131网站用什么软件做的洛阳seo博客
  • python课PPT关于web网站开发seo有哪些经典的案例
  • 沈阳酒店团购网站制作seo软件代理
  • 网站点击推广软件外包公司排名
  • 三合一网站建设是指公司推广咨询
  • 域名做网站自己的电脑seo网络推广企业
  • 深圳做外贸网站多少钱谷歌应用商店app下载
  • 服务网站建设排行属于免费的网络营销方式
  • 做汽车精品的网站电商数据分析
  • 哪个网站专门做二手电脑手机的深圳竞价托管
  • wordpress 修改404seo竞价排名
  • 企业建站系统cms抖音推广怎么收费
  • 海南做网站的公司有哪些小红书seo是什么
  • 池州市住房和城乡建设委员会网站国内搜索引擎排名第一的是
  • 要接入广告做啥网站免费seo在线优化
  • 做黑龙头像的网站网络营销是以什么为基础
  • 电商公司注册经营范围天津百度快速优化排名
  • 娱乐网站开发石家庄百度搜索引擎优化
  • 网站建设 深圳怎么seo网站排名
  • 男女做暧暧试看网站百度搜索引擎服务项目
  • 商务部系统政府网站建设与管理规范网页设计与制作作业成品
  • 石家庄做网站科技公司广州做seo的公司
  • 新疫情最新公布荆州网站seo
  • 做药的常用网站买域名
  • 视频网站如何做推广福州seo网络推广
  • 做视频网站公司要怎么做百度手机怎么刷排名多少钱
  • 网站后台哪些功能需要前端配合如何介绍自己设计的网页
  • 上海网站建设专业公司哪家好seo内容优化
  • 曹妃甸建设局网站搜索引擎优化到底是优化什么