Meaning of Exclamation mark

Hi, I couldn’t exactly understand the meaning of (!hasStarted). As far as I understood this means “Not hasStarted”. But what exactly does that mean? Does that mean hasStarted = true?
Instead of (!hasStarted) I tried (hasStarted = true) , (hasStarted = false) and (hasStarted) and in all three cases the ball dropped from the top of the paddle as soon as I hit play. So which statement can be used instead of (!hasStarted)? Thanks.

Hi Ahmet,

That’s correct.

No, other way round, hasStarted = false.

The long way of writing the same condition would be;

if(hasStarted == false)
{
    // ...
}

The if condition is evaluation for true, e.g.

if(hasStarted)
{
    // ...
}

the above is the same as saying;

if(hasStarted == true)
{
    // ...
}

So, when you have !hasStarted you are effectively saying, “hasStarted isn’t equal to true”, as a boolean value, if it isn’t equal to true, then it is equal to false.

Make sense?

Thanks for the clear explanation Rob, I think I got it now. The reason I got confused during the lecture was that we initialize the hasStarted value as false so I wasnt sure whether if (hasStarted) meant true or false. I also used a single equal sign (=) instead of a double one (==) when I tried to write the code in the long way so thats why mine didnt work. Thanks again for the assistance.

1 Like

Hi,

You’re very welcome.

Regarding booleans, by default they will be false however it is sometimes useful for clarity/readability to specifically state that when you declare your variables - not entirely necessary though.

For the equals operator, when you are initialising a variable you use one, if you are performing a comparison, use two;

declaration with initialisation

private string greeting = "Welcome Ahmet!";

comparison

if(playerLives == 0)
{
    // ...
}

Hope this helps :slight_smile:

Privacy & Terms