Creating a For loop Menu and understanding it

I created a for loop menu because I am trying to understand when not to use specific loops and when to use specific loops. I know a do while loop are best for this type and would work better. I feel that learning when and when not to use them by using them to see how much trouble it really is makes better for understanding C++.

Question 1 - I'm not understanding why i!=4 would loop unless I type 3 but wouldn't it be if I type 4 exit everything else loops?

Question 2 - for some reason int i; is placed before the loop instead of for(int i; etc; etc) I don't understand because the formula is
for ( variable initialization; condition; variable update )
{
// Code to execute while the condition is true
}
I feel like I'm missing something here...

example code 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>

using namespace std;

int main()
{
    int i;
    for (i=i; i!=4; i++)
    {
        cout << "Choose from Below: \n";
        cout << "-------------------------------\n";
        cout << "|                             |\n";
        cout << "| 1. Addition                 |\n";
        cout << "| 2. Minus                    |\n";
        cout << "| 3. Exit                     |\n";
        cout << "|                             |\n";
        cout << "-------------------------------\n";
        cin >> i;
        if (i == 1)
        {
            cout << "Addition Stuff Here\n\n";
        }
        else if (i == 2)
        {
            cout << "Minus Stuff Here\n\n";
        }
        else if (i == 3)
        {
            cout << "Exiting - Good Bye!";
            break;
        }
        else
        {
            cout << "This choice is unavailable at this time";
        }
    }
}
it could be for(;;) it would still work
Last edited on
It's a bit confusing because of the i++ in your for loop.

Here's what's happening:
1. At line 18 you enter 3 (i becomes 3)
2. At line 29 you print "Exiting - Good Bye!"
3. At line 36 you go to the top of the loop
4. At line 8 you execute i++ (i becomes 4)
5. At line 8 you evaluate i!=4. it's false
6. You exit the loop.

Remove i++ and you'll be ok.
Last edited on
On line 8 you probably meant to set i to one (1) instead of i.
perfect I understand now! I thought you had to have information such as the variable initiation condition and variable update. but really i could just keep it blank just as long as i have the ; to delineate each segment of the for code. taking i++ and changing i!=4 to i!=3 also fixed the issue so I can take the break out where i == 3.

So whith that in mind my fix thanks to you has become

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

using namespace std;

int main()
{
    int i;
    for (; i!=3;)
    {
        cout << "Choose from Below: \n";
        cout << "-------------------------------\n";
        cout << "|                             |\n";
        cout << "| 1. Addition                 |\n";
        cout << "| 2. Minus                    |\n";
        cout << "| 3. Exit                     |\n";
        cout << "|                             |\n";
        cout << "-------------------------------\n";
        cin >> i;
        if (i == 1)
        {
            cout << "Addition Stuff Here\n\n";
        }
        else if (i == 2)
        {
            cout << "Minus Stuff Here\n\n";
        }
        else if (i == 3)
        {
            cout << "Exiting - Good Bye!";
        }
        else
        {
            cout << "This choice is unavailable at this time";
        }
    }
}


Thank you for helping me understand and solving my issue! One last question currently I have C++ Programming for Beginners and Jumping into C++ as learning tools and references. Is there another book I should be looking to get?
You don't need a book if you consistently think of the next thing you could try out. It's all about practice and trying new things.
FWIW:

1
2
    int i;
    for (; i!=3;)


Note that 'i' is not initialized, so it could be any value. So here it might be 3... in which case your program will just skip over that entire loop.

Always initialize your variables before you attempt to read them!


Also...

A for loop with no initialization/increment potion is identical to a while loop.

1
2
3
4
5
// this:
for (; i!=3;)

// is identical to this:
while( i!=3 )


In general... you use a for loop when there's a fixed number of times you want to loop (ie: Do something 100 times). Or if there's a specific container you want to iterate over (ie: Print every element in my array/vector).

You use while - and occasionally do/while - to loop when the condition is not determined by a count, but rather is a state change or event (ie: loop until the user presses enter. Or loop until the player got game over. Or loop until an external process shut down)
Topic archived. No new replies allowed.