首页 > 后端开发 > C++ > 正文

在C/C++中同时执行if和else语句

PHPz
发布: 2023-09-05 14:29:06
转载
1769人浏览过

在c/c++中同时执行if和else语句

在本节中,我们将了解如何在 C 或 C++ 代码中同时执行 if 和 else 部分。这个解决方案有点棘手。

当 if 和 else 相继执行时,就像执行不存在 if-else 的语句一样。但在这里我们将看看它们是否存在如何依次执行它们。

示例代码

#include <iostream>
using namespace std;
int main() {
   int x = 10;
   if(x > 5)   {
      lebel_1: cout << "This is inside if statement" <<endl;
      goto lebel_2;
   }else{
      lebel_2: cout << "This is inside else statement" <<endl;
      goto lebel_1;
   }
}
登录后复制

输出

This is inside if statement
This is inside else statement
This is inside if statement
This is inside else statement
This is inside if statement
This is inside else statement
This is inside if statement
This is inside else statement
....
....
....
登录后复制

该程序将充当无限循环,但这里的 if 块和 else 块同时执行。第一次检查后,条件检查对输出并没有真正的影响。

注意:这里我们使用 goto 语句强制将 if 块中的控制发送到 else ,然后 else 发送到如果。但使用goto语句并不好。这使得追踪程序的控制流程变得困难。

立即学习C++免费学习笔记(深入)”;

以上就是在C/C++中同时执行if和else语句的详细内容,更多请关注php中文网其它相关文章!

c++速学教程(入门到精通)
c++速学教程(入门到精通)

c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
相关标签:
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号