Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
Why don't we just set it to true - why are we using the complier to change the value?

That's certainly a free choice. However, in general, you don't know if the user wants the value to be true or false. (Once you can handle user input and are not explicitly setting variables in your code [a.k.a. hardcoding]).

Code:
itsARerun = true;

would be equivalent to:

Code:
itsNotARerun = false;

And the double negative can make the code harder to interpret down the line. For example, if you presented the user a check box for "Is a rerun?" you would have to invert or negate that to put it in the variable.

So ultimately, it depends on your interfaces and where the data will be coming from and also influences readability and maintainability.

However, if your data source has a flag or you input a flag for if the program is first run, you might define the variable as

Code:
itsFirstRun = true;

B
 
No what I meant - why are we asking the complier to change the value using the ! operator.

Code:
itsARerun =false;

if ((!itsARerun)

This part of the code changes the false to true.

Why don't we just set it to true - why are we using the complier to change the value?
Don't get caught up in the simple example. It's just a simple example. Reality is almost never that simple. The real world rarely consists of named boolean variables.

The most common use of relational operators is when making comparisons. The ! operator can be considered a relational operator, though it has other uses.

A typical real-world use of ! is when a function returns a boolean value, and you want to test its opposite. So someFunc() returns true when a condition prevails, but you want a block to be performed when the condition is false. So:
Code:
if ( ! someFunc( someVariable, etc ) )
{  .. do something when false .. }

One non-relational use of ! is to invert the true/false state of a variable, i.e. toggle it. This isn't just a user-interface thing, with toggle buttons or checkboxes. If you can't think of an example, don't worry about. Eventually you'll see one. I could post one from the code I'm working on now, but I'd have to find one first, and then explain the context. I'm inadequately caffeinated for that task at the moment.

The operators are not all used at the same frequency. Some are used more than others. In my experience, ! is used less than < or > or == or != or && or ||.
 
Last edited:
someFunc() returns true when a condition prevails, but you want a block to be performed when the condition is false.

Of course as in this simple example where you want two different things to happen depending on the value of the boolean, you can also swap the if and else to achieve a similar effect.

B
 
And the double negative can make the code harder to interpret down the line.
Or even the first time.

Rereading the original posted code:
Code:
	bool    nothingElseOn, itsARerun;
	
	nothingElseOn = true;
	itsARerun = true;
	
	if ( nothingElseOn || (! itsARerun) )
		printf( "Let's watch Family Guy!\n" );
	else
		printf( "Something else is on or I've seen this one.\n" );
If the interpretation of itsARerun is intended to correspond to "I've seen this one", then I think this code is wrong, in the sense of not matching the logic expressed in the message texts. The ! should be removed, and I also suspect the || is wrong.

If the entire test is reversed, using the Boolean algebra for negation, I'm pretty sure we get this:
Code:
	if ( (! nothingElseOn) && itsARerun )
		printf( "Something else is on or I've seen this one.\n" );
	else
		printf( "Let's watch Family Guy!\n" );
Here, "Something else is on" logically corresponds to (! nothingElseOn), and "I've seen this one" also corresponds to itsARerun, however, the && doesn't match the "or" in the string. (In boolean algebra, the logical NOT of AND is OR, and vice versa, i.e. NOT AND is OR, and NOT OR is AND.)

Naming the variable nothingElseOn was also a poor choice, akin to choosing itsNotARerun.

So let's revise it to improve the variable naming and the logic.
Code:
	bool    somethingElseIsOn, iveSeenIt;
	
	somethingElseIsOn = true;
	iveSeenIt = true;
	
	if ( somethingElseIsOn || iveSeenIt )
		printf( "Something else is on or I've seen this one.\n" );
	else
		printf( "Let's watch Family Guy!\n" );
Now, if we reverse the whole conditional:
Code:
	if ( (! somethingElseIsOn) && (! iveSeenIt) )
		printf( "Let's watch Family Guy!\n" );
	else
		printf( "Something else is on or I've seen this one.\n" );
You can read this as "If something else is not on, and I have not seen it, let's watch Family Guy". You can further simplify "something else is not on" to "nothing else is on".

Again notice the && here, not an ||. Which further reinforces my thinking that the code logic doesn't match the logic expressed in the text messages.


To the OP and any other interested reader:
Yes, this is how real-world code is designed. You first have to correctly understand the real-world expression and operation of something, also known as How It Works In Reality. Then you have to correctly translate that into code, or How The Software Works (or doesn't). Too many people skip the first part, and the resulting code is either wrong, or works for the wrong reason. If it works for the wrong reason, a subtle change in the real world can cause the code to stop working, even when it should still work.
 
That's certainly a free choice. However, in general, you don't know if the user wants the value to be true or false. (Once you can handle user input and are not explicitly setting variables in your code [a.k.a. hardcoding]).

Okay this makes sense. I was looking at the code and not thinking about the interface!

So ultimately, it depends on your interfaces and where the data will be coming from and also influences readability and maintainability.

However, if your data source has a flag or you input a flag for if the program is first run, you might define the variable as

Code:
itsFirstRun = true;

B

So it just depends what the program needs to do in order to determine which method I use. Okay, makes sense - thank you. :cool:


To the OP and any other interested reader:
Yes, this is how real-world code is designed. You first have to correctly understand the real-world expression and operation of something, also known as How It Works In Reality. Then you have to correctly translate that into code, or How The Software Works (or doesn't). Too many people skip the first part, and the resulting code is either wrong, or works for the wrong reason. If it works for the wrong reason, a subtle change in the real world can cause the code to stop working, even when it should still work.

Your post was very helpful. :cool:

Once I have finished the book - I will look at a flow chart to designing a program from scratch and see which method would suit me better.

Again, thanks guys!

----------

chown33 - I had the same thoughts as you - the original code wasn't making much sense to me. Your version simplifiers it a lot more!
Thanks!
 
Another question I have - is this bit of code:

Code:
int main (int argc, char const * argv[])

I have not seen this function prototype explained properly in the book. What does this code do exactly and why is it at the beginning of every program?

I worked out that it allows the program to start - but inside the ( and ) what does that part do? (I understand the "int main" part)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.