whats the difference with these 2

Apr 13, 2013 at 3:52am
Did an exercise once and still felt a little confused so I did it again.
The second time around it wouldn't work properly with out the breaks.

Not a big deal but I'm wondering why that is?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main()
{
    int input, x;
    for (x = 0; x < 10; x++)
    {
        cout << "Please enter any number except 5 \n";
        cin >> input;
        cout << "You entered: " << input << endl;
        if (input == 5){
            cout << "Hey! I told you not to enter 5! \n";
        }
            else if (x == 9){
            cout << "Wow, you're more patient then I am, you win. \n";
        }

    }

    return 0;

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>


using namespace std;

int main()
{
    int input, counter;
    for (counter = 0; counter < 10; counter++){
        cout << "Please enter any number except 5. \n";
        cin >> input;
        cout << "You entered " << input << endl <<endl;
        if (input == 5){
            cout << "Hey I told you not to enter 5! \n";
            break;
        }
        else if (counter == 9){
            cout << "WoW! You have more patience then I do. You win! \n";
            break;
        }
    }

    return 0;
}


I'm not seeing a difference
Apr 13, 2013 at 4:16am
closed account (3CXz8vqX)
Really? You mean apart from the break? The break prevents looping by the looks of things. So if you meet either of the conditions you exit the loop.

The same cannot be said for the above example.

Last edited on Apr 13, 2013 at 4:18am
Topic archived. No new replies allowed.