Solved

if (variable) and if (!variable)

Posted on 2001-07-26
10
276 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Picking random number 8 169
Having a file "symlink" to a memory location (Windows) 6 160
stack 22 156
Where is my core dump file in Ubuntu? 12 428
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 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.

758 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

20 Experts available now in Live!

Get 1:1 Help Now