
在本节中,我们将了解如何在 C 或 C++ 代码中同时执行 if 和 else 部分。这个解决方案有点棘手。
当 if 和 else 相继执行时,就像执行不存在 if-else 的语句一样。但在这里我们将看看它们是否存在如何依次执行它们。
示例代码
#include 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 statementThis is inside else statementThis is inside if statementThis is inside else statementThis is inside if statementThis is inside else statementThis is inside if statementThis is inside else statement............
该程序将充当无限循环,但这里的 if 块和 else 块同时执行。第一次检查后,条件检查对输出并没有真正的影响。
注意:这里我们使用 goto 语句强制将 if 块中的控制发送到 else ,然后 else 发送到如果。但使用goto语句并不好。这使得追踪程序的控制流程变得困难。
立即学习“C++免费学习笔记(深入)”;
以上就是在C/C++中同时执行if和else语句的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1444417.html
微信扫一扫
支付宝扫一扫