true false help? if (!variable){}

Im trying to figure out how to use an SD card but i want to understand the example sketches first.

The sketches have a part: if (!SD.begin(4)){ "it did not work"}

i was playing around and changed it to: if (SD.begin(4)){ "it does work"}

but when the card does initialize it does not show "it does work" why? i do not understand what if (!SD.begin(4)) means? especially the ! mark. and why can i not use this instead:

if (SD.begin(4) == 0){ "it did not work"}
if (SD.begin(4) == 1){ "it did work"}

is 0 false and 1 true? i have read that if a program executes successfully it returns a 0? but this is contradicting because 0 is false?

Please help im getting very frustrated:(

SD.begin() returns a boolean. If the code you modified does not show the message you expect, you need to post all of your code, not just a little snippet.

SD.begin() returns true on success and false on failure. And ! is the logical negation operator in C/C++. So the expression !SD.begin() returns true on failure, exactly what you want to print an error message.

The statement...

if (!SD.begin(4)){ "it did not work"}

does not do anything. The complier will accept this as valid code however if you want the text "it did not work" output somewhere you need to explicitly do so. For example...

if (!SD.begin(4)){Serial.println( "it did not work");}

PaulS:
SD.begin() returns a boolean. If the code you modified does not show the message you expect, you need to post all of your code, not just a little snippet.

What is a boolean? im not concerned about my code, the examples i posted are just examples and not correct programing language. i just want to understand the concept.

if the SD card does initialize on pin 4 does SD.begin(4) return a 0 as true? and if it does not initialize on pin 4 then does SD.begin(4) return a 1 as false?

when i connect pin 4 to the SD module and my code includes the following then both messages "YES" and "NO" are displayed but why????? only yes is suppose to display

if (SD.begin(4)) {
Serial.println("YES");
}

if (!SD.begin(4)) {
Serial.println("NO");
}

What is a boolean?

http://arduino.cc/en/Reference/BooleanVariables

if the SD card does initialize on pin 4 does SD.begin(4) return a 0 as true? and if it does not initialize on pin 4 then does SD.begin(4) return a 1 as false?

No. It returns true or false. Do not be concerned about the values that correspond to true or false.

when i connect pin 4 to the SD module and my code includes the following then both messages “YES” and “NO” are displayed but why??? only yes is suppose to display

Why do you think that? The first call to begin() succeeded, so YES got printed. The second one failed, because the card is in use, so NO got printed.

Why do you say i must not be concerned with the values 0 and 1 that correspond to true and false ? im not asking this in a cocky way im just confused now. i thought this is how you test if something is sucessful or not

I must obviously know if the sd card initialized. if i get a 1 it did, if i get a 0 it did not?

If you print true, it is true you get a 1. But that doesnt mean 1 is true and 0 is false. Actually it is 0 is false and anything that is not 0 is true.

Delta_G:
If you print true, it is true you get a 1. But that doesnt mean 1 is true and 0 is false. Actually it is 0 is false and anything that is not 0 is true.

ok so if something does not work it returns a false value which is not necessarily a 1? can it be any number besides 0?

False is zero.
It cannot be any other value

i see my previous post makes no sense. so false = 0. and true = any number besides zero? is this correct? so true can be 1 or 5 or 2878484758 or 9932048412392985129385?

calvingloster:
i see my previous post makes no sense. so false = 0. and true = any number besides zero? is this correct? so true can be 1 or 5 or 2878484758 or 9932048412392985129385?

Correct, if it's not zero then it's true.