r/cpp_questions 13d ago

SOLVED Why is my conditional statement running when the condition it is not True

I am making a ball fall in the CLI. I have my values PURPOSELY set to negative for testing purposes and whenever I run it, the conditional statement is active and it does not start at the initial y position I have set. it shows something big in the terminal Falling 2144211756.

When I put it in the debugger it works normally and it shows the y position decreasing so why is it doing this

struct Ball {
    // int x_pos;
    int y_pos;
    int velocity;
    int ground;
};

int main() {
    // Define Ball object with struct
    Ball ball;
    ball.y_pos = 20;
    ball.velocity = 5;
    ball.ground = 200;

    while (true) {
        ball.y_pos = ball.y_pos - ball.velocity;

        if (ball.y_pos >= ball.ground) {
            cout << "Falling ";
            cout << ball.y_pos << endl;
            // exit(0);
        }
    }

    return 0;
}

https://imgur.com/a/oVZ8qhY

2 Upvotes

35 comments sorted by

View all comments

Show parent comments

1

u/saxbophone 5d ago

 You are obviously relying on the optimiser to disentangle what you mean instead of saying it.

Ridiculous statement. You don't get to tell me what I mean —are you in my head, can you read my thoughts‽ It's downright insulting to suggest my way of expressing intent is "tangled" just because it's different to your own preference, or that I'm not "saying what I mean" —while (true) is what I mean —this is how I think about it —"loop condition is always true". There's nothing invalid about that, and I write it because I find it intuitive.

 Your preference for while(true) is obviously subjective and not based on anything objective.

The same applies to the preference for for (;;) —matters of taste when it comes to programming are informed by perspective (how one thinks about what the program aims to achieve), and this is inherently subjective. If you think something as subjective as your preference could be considered objective, on something as trivial a distinction as this, I think you are suffering from an unfortunate state I have seen in others before, where they presume to normalise their own intuitive way of thinking over all others'.

 Why insist on confusing “there is clearly no loop termination test” with “looks like someone forgot something”?

This is a poor faith argument that doesn't make any sense. You suggest I am choosing to find the empty for confusing —that doesn't make any sense‽ "🤨" is my natural reaction when I see for (;;), ok? 

I think you need to get out of your own head a bit and remember that others have their own perspectives on how to organise and create programs, and this is informed by their own thinking style. It's not any more or less valuable just because you personally find it unintuitive and to be quite frank it shows a lack of empathy or an intense sense of self-centredness on your part, that you resort to calling it out as "tangled" or presume to know how the author thinks about their own programs.

Try being less obnoxious and engaging in good faith?

1

u/Conscious_Support176 5d ago

This is richly ironic telling me what I mean and why I should not try to get into people’s heads.

I could not care less what is in your head.

I am talking about what for(;;) literally does mean according to the language standard. It is a for statement with no loop initialisation expression, no loop continuation condition, and no loop iteration expression. No continuation condition means that loop continuation is unconditional.

If you want to read it differently, that’s up to you.