Solved

if (variable) and if (!variable)

Posted on 2001-07-26
10
277 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
  • 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
 

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

Superior storage. Superior surveillance.

WD Purple drives are built for 24/7, always-on, high-definition security systems. With support for up to 8 hard drives and 32 cameras, WD Purple drives are optimized for surveillance.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

896 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now