Solved

if (variable) and if (!variable)

Posted on 2001-07-26
10
281 Views
Last Modified: 2010-04-15
Hello all,

I am an (extreme) novice at C and any other kind of programming. I would like to know what do these two blocks of code do:

if (variable) {

   [some statements]

}

and

if (!variable) {

   [some statements]

}


I would also like to know what is the difference between them.

TIA
0
Comment
Question by:gfajdi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
10 Comments
 
LVL 5

Accepted Solution

by:
scrapdog earned 50 total points
ID: 6323090
! is the boolean "not" operator


if (variable)  means:

"If the variable evaluates to 'true', then..."



if (!variable) means:

"If the variable evaluates to 'not true' (i.e. false), then..."


Nonzero values are considered 'true'.  Values that are 0 are considered 'false'.
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 6323093
When you see the ! in C code, think of it as the word "not".
0
 

Author Comment

by:gfajdi
ID: 6323328
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:gfajdi
ID: 6323428
0
 

Author Comment

by:gfajdi
ID: 6323447
So in other words:

if (variable)

is the same as

if (variable != 0)


and

if (!variable)

is the same as

if (variable == 0)


am I correct
0
 

Author Comment

by:gfajdi
ID: 6323668
So in other words:

if (variable)


is the same as

if (variable != 0)


and

if (!variable)

is the same as

if (variable == 0)


Am I correct?
0
 

Author Comment

by:gfajdi
ID: 6323728
0
 

Author Comment

by:gfajdi
ID: 6323865
Sorry for that I'm having some connection problems
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 6323937
>if (variable)
>is the same as
>if (variable != 0)
>
>and
>
>if (!variable)
>is the same as
>if (variable == 0)
>
>Am I correct?

Yes, that is correct.
0
 

Author Comment

by:gfajdi
ID: 6323984
Thanks, you have answered my question, here are your well-deserved points.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
memory leak detection 9 113
Finding a good hash function 4 132
convert char array to number in c 5 93
Exchange 2013 fills up C drive with logs 4 29
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question