This is what I have so far :/

#include <iostream>


using namespace std;


int main()
{

    int i;

    for (i = 1; i <= 12; i++)
    {
        cout << i;
        if ((i = 4) && (i = 9)){
            cout << " \n";
        }
        else {
            cout << endl;
        }
    }










    system("pause");
    return 0;
}

Dani AI

Generated

A few focused notes that clarify what went wrong and how to fix it cleanly.

The original loop had two separate issues: using the assignment operator (=) inside the if and using && where || was intended. As hinted, expressions like (i = 4) && (i = 9) assign to i rather than compare it; the assignments change i during the condition evaluation (it ends up as 9), so the loop increment leaves i at 10 repeatedly — the cause of the “keeps printing 10” behavior saw. As noted, == is for comparison. As pointed out, to exclude multiple values the logical OR (||) is the correct operator (an int cannot be both 2 and 9 at the same time).

A simple, readable pattern is to skip unwanted values with continue:

#include <iostream>

int main() {
    for (int i = 1; i <= 12; ++i) {
        if (i == 2 || i == 9) continue;
        std::cout << i << '\n';
    }
}

For a scalable solution (many excluded values) use a small container:

#include <iostream>
#include <unordered_set>

int main() {
    std::unordered_set<int> skip{2, 9};
    for (int i = 1; i <= 12; ++i) {
        if (skip.count(i)) continue;
        std::cout << i << '\n';
    }
}

Final tips: avoid system("pause") (Windows-only) and avoid using namespace std; at global scope. Prefer '\n' over std::endl unless an immediate flush is needed. Keep condition logic simple and comment intent — that prevents the common = vs == and && vs || mix-ups that prompted this thread.

Recommended Answers

All 7 Replies

It just loops number 10 for some reason

Member Avatar for Member #248612

(i = 4) && (i = 9) does not do what you think it does

  • As was noted, you're using the assignment operator(=) not the equality operator(==) to test for the numbers you want to exclude.

  • Also your description says you want to exclude 2 and 9 but your code seems to indicate 4 and 9.

  • Your logic is a bit skewed line 14(cout << i;) should be part of line 19(cout << i << '\n';).

I changed everything to this

#include <iostream>


using namespace std;


int main()
{

    int i;

    for (i = 1; i <= 12; i++)
    {
        if ((i == 2) && (i == 9)){
            cout << " \n";
        }
        else {
            cout << i << '\n';
        }
    }










    system("pause");
    return 0;
}

still... it doesn't exclude 2 and 9

cout << " \n"; doesn't include it because it's printing only an empty line on that statement?

Oh wait, why are you using && operator? i cannot be 2 and 9 at the same time ... did you mean ||?

#include <iostream>
using namespace std;
int main()
{
    int i;
    for (i = 1; i <= 12; i++)
    {
        if ((i == 2) || (i == 9)){
            cout << " \n";
        }
        else {
            cout << i << '\n';
        }
    }
    return 0;
}

OMG... my teacher said it means "and" for some reason.

EDIT: This works! thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.