# BOOL confuses me

Am I understanding the deal with the Logical NOT. The whole “true becomes false, false becomes true” thing seems kinda confusing. Can you explain it a little bit more…? I’d appreciate it… Here’s the BOOL part of the code (To save time, I didn’t write the part where the truck in question is 35000…)

BOOL isNotLegal = !((truckWeight > 0.0) && (truckWeight < 40000.0));
if (isNotLegal) {
printf(“Truck weight is not within legal range.\n”);
}

I mean, why would anyone go to all this trouble for a variable to simply hold a false? If the truck is 35000, then the truck is greater than 0 AND it’s less than 40000, so the statement is true. But the ! at the front of the expression means you’re supposed to make the true into false, so the overall statement is false, right? Therefore, in this case, the printf statement above would not print, right? I tested this and indeed it did not print it. I then deleted the ! and the program printed “Truck weight is not within legal range” like I expected. Guess I’m just wondering how popular the BOOL variable is because it seems like a complicated way to get a false. Am I just over-thinking this?

Thanks!
-Buzz

That BOOL variable makes the code more readable. Plus you might also use it again in the same context later on.

``````BOOL isNotLegal = !((truckWeight > 0.0) && (truckWeight < 40000.0));
...
if (isNotLegal) {
printf ("Truck weight is not within legal range.\n");
}
...
if (isNotLegal) {
printf ("Confiscate it...\n");
}
...``````

Compare it to this:

``````...
if (!((truckWeight > 0.0) && (truckWeight < 40000.0))) {
printf ("Truck weight is not within legal range.\n");
}
...
if (!((truckWeight > 0.0) && (truckWeight < 40000.0))) {
printf ("Confiscate it...\n");
}
...``````

Thanks for that.
-Buzz

so if i understand correctly the “!” is useful when you have a statement that was created at some point as true and instead of rewriting everything to make it false all you have to do it place the “!” in front of it ?

thanks mike

[quote=“iOSMikey”]so if i understand correctly the “!” is useful when you have a statement that was created at some point as true and instead of rewriting everything to make it false all you have to do it place the “!” in front of it ?

thanks mike[/quote]

Ummm OK - Let’s see if I can help… The BOOL is like a switch, it can hold either a True or a False. In this example they are also including the ! which as you know means NOT. So this code actually means, if the trucks weight is NOT between 0.0 or 40000.0 then BOOL is true and you’ll see the “…not within legal range.” warning.

The example code actually saves you having to use an ELSE as well. While you could use BOOL to hold the true value if the truck was between the two weights, you would also need to tell the code what to do if the truck is NOT in the legal range. Since we don’t need to do anything if the truck is legal - this example code works perfectly for us.

So using the example code, if a “Legal weight” truck passed by then nothing would happen. Try changing the weight to something outside the range and you’ll see the warning.

You have to remember that these examples are to allow you to grasp the concepts of the code. In this case, you need to imagine that you are writing a program to determine if a truck is NOT within the weight range and remember that the weight is not always going to be 35000.00.

Hope this helps?

When writing programs, I feel it is better not use the NOT logic.

While it may make your code longer, it is more understandable when you (or someone else) goes back to maintain it or needs to review the code to determine why something didn’t work right.

It is much easier to understand:

``````BOOL isTruckLegal;
int truckWeight = 35000;

if ((truckWeight > 0) && (truckWeight <= 40000)) {
isTruckLegal = 1;
} else {
isTruckLegal = 0;
}

//  Some other code

if (isTruckLegal) {
printf("The truck is OK.  Let it through!\n");
} else {
printf("Stop that truck!  It's too heavy for these roads!\n");
}
``````