goto语句的另一种方法在C ++ [英] Alternative way for goto statement in C++

查看:2036
本文介绍了goto语句的另一种方法在C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是流程图的链接:
http://i1146.photobucket.com/albums/o530/HTHVampire/C%20plus%20plus/Capture_zps5a6c3acf.jpg





这是流程图的代码,如图所示,只是忽略流程图中的模糊语句。

  #include< iostream> 
using namespace std;

int main()
{
//声明并初始化变量
int a = 0;
//提示用户输入值
cout<< 请输入值< endl;
cin>>一个;

again1:
//输入决策块
if(a> 10)
{
if(a <10)
{
again2:
if(a< 100)
{
a = a - 3;
goto again2;
}
else goto again1;
}
else
{
a = a - 7;
goto again1;
}
}
else cout<< 输出是< a<< endl;

return 0;
}

我可以知道我可以用if-else语句while语句?

解决方案



<该结构应当根据流程图执行核心逻辑:

  while(a> 10){
if a< 10){
while(a <100){
a + = 3;
}
} else {
a - = 7;
}
}

注意 if test是荒谬的。但是,我没有画出流程图;我刚刚在代码中复制。


Here's the link for the flowchart: http://i1146.photobucket.com/albums/o530/HTHVampire/C%20plus%20plus/Capture_zps5a6c3acf.jpg

Here's the code for the flowchart as shown, just ignore the ambiguous statement in the flowchart.

#include <iostream>
using namespace std;

int main()
{
    //declare and initialize a variable
    int a = 0;
    //prompt user for a value
    cout << "please enter a value" << endl;
    cin >> a;

    again1:
    //enter a decision block
    if(a > 10)
    {
        if(a < 10)
        {
            again2:
            if(a < 100)
            {
                a = a - 3;
                goto again2;
            }
            else goto again1;
        }
        else
        {
            a = a - 7;
            goto again1;
        }
    }
    else cout << "the output is " << a << endl;

    return 0;
}

May I know that can I play this code with if-else statement together with while statement? instead of goto statement.

Thanks for your guides!

解决方案

This structure should do the core logic according to the flowchart:

while (a > 10) {
    if (a < 10) {
        while (a < 100) {
            a += 3;
        }
    } else {
        a -= 7;
    }
}

Note that the if test is absurd. However, I didn't draw the flowchart; I just reproduced it in code.

这篇关于goto语句的另一种方法在C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆