How does while (!WindowShouldClose()) work?

So the WindowShouldClose function always returns a value of false until X or the escape button is pressed, and while (WindowShouldClose) returns false the while loop continues. That makes sense… why does flipping the value with a while (!WindowShouldClose()) make any sense at all?

If (!WindowShouldClose()) only flips the truth value to false then wouldn’t hitting escape just turn the value to truth only to then be flipped back into false and so always return false and thus never actually stop the while loop… what? This all made sense until this Negation Operator was introduced.

Edit: And also, while the while (!WindowShouldClose()) is returning false like it would be most of the time it would return the value of true and so shouldn’t the window close the moment it opens because the while (!WindowShouldClose()) function would immediately return false but then be flipped to true by the ! and close the window? Obviously this works, but I don’t know why it works.

WindowShouldClose ( ) function is not set to true when it’s initialized with the ! before it. Its literally initialized as “NOT true” because of the ! in front. (It doesn’t switch states) from true to false when it’s initialized. If it did, then yes, window would close right away. It’s set to “true” only when the X button is clicked or ESC key.

The ! in front is like saying

while ( WindowShouldClose(false) )
{
game code here
{

but this function (I don’t think) can take an argument so you either have to have it with a ! in front or not. You can’t put (true) or (false) inside (I don’t think). It defaults to “true” with just the (). The ! in front makes it a NOT TRUE from the moment it’s initialized

In other words

while ( WindowShouldClose(false) )

is the same thing as

while ( !WindowsShouldClose(true) )

which is the same thing as

while ( !WindowsShouldClose() )

The way I think of it with the ! in front it’s like if there was a function called “WindowShouldNotClose()” = true all the time (until x is clicked or ESC pressed). And while its in this state, leave window open. (That function doesn’t exist but if it did that’s how it would work.) Clicking X or hitting ESC immediately changes this to false which means it closes within a frame since the while loop executes all the code in the while loop every frame.

The key to understanding this is the ! in front means “NOT”

For the record, WindowShouldClose() does not take an argument as an input. That said, the general idea is correct.

In a code-accurate sense, !WindowShouldClose() is the equivalent of WindowShouldClose() == false. The reason why this works is that an if-statement only executes the code contained within if the statement resolves as true. So as jfschafer says, with use the ! as a short-form for reversing the result of a statement. So a result of “true” would be “false” instead.

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms