-3
x = 1 

if(x) {
 x equals true
}

but what happens when you put the ! infront:

if(!x) { 
x equals ? 
}

I see it being used lots in tutorials I read and felt I understood it. But I saw it today and it confused me again.

What does it do? What is it's purpose? why would you use it?

  • 2
    It means "if x is not true, execute these statements". – deprecated Nov 18 '15 at 11:44
  • 1
    `!` means `not`. So `not true - false`, `not false - true`. if I don't mistake, in `c` language `0 is false` and `whatever is true`. In JS `1 is not 0` so its `true`. `true is converted to 1` so its `true` again... – Valijon Nov 18 '15 at 11:44
  • you should have done some google before asking. – Suchit kumar Nov 18 '15 at 11:46
  • Which language? The behaviour of `!` can be changed in some. This is not answerable without a language tag. – Bathsheba Nov 18 '15 at 11:47
  • Did you try `x = 17;` in the first line? – CiaPan Nov 18 '15 at 11:48
  • The first thing I do is google search, stack overflow is a last option for me. It amazes me that you can just decide what I've been doing before I ask a question on here. I don't get the need for your comment it's a genuine question for me – mike beckett Nov 18 '15 at 11:51
  • 1
    Dude, tag the language. Pretty please, with sugar on top. – Bathsheba Nov 18 '15 at 11:52
  • 1
    Well thanks to everyone who has tried to help me and I genuinely mean that, I appreciate it. Unfortunately I can no longer post so I will no longer use stack overflow. Just really really hope the guys who can't get through the day without down-voting. Continue to enjoy themselves. For me I asked genuine questions, that as a beginner I didn't understand. No one says what your doing wrong, just down voting and assuming that people are being lazy because they don't have the knowledge you have. Well your mission was a success, I hope you enjoy it. Meanwhile I'm a beginner banned for a question – mike beckett Nov 18 '15 at 15:25

2 Answers2

1

The exclamation mark merely means not that is a boolean negation, so

if(!x)
{
    (not x) is true, which means x is false
}

For ordinal types of x that means x == 0, for pointers x == NULL.

CiaPan
  • 9,381
  • 2
  • 21
  • 35
0

This means if x==0 or x== false or not x as where !x