Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

enter image description here

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
268 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...