Mac What is this' ! '

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
The book tossed this one out at me in code and didn't explain it.

Code:
 while (!validInput);
I don't know why he put the ! in front of validInput

Does it do anything?

-Lars
 

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
hey thanks. That is the one thing about this book, the author just tosses in something with out explaining it. He might explain it later in the book like he has with other things but people who are learning, Java in this case, like to analyze everything and when something like that pops up I wonder what it does. Now I NOT know what it is, haha ! = NOT. Anyways it's late and that was my first programmers joke ever and a bad one I am sure : )

-Lars
 

iMeowbot

macrumors G3
Aug 30, 2003
8,636
0
The world is filled with books like that! I do wonder how many textbooks are ever shown to people who don't know the subject yet, to see if they make any sense before they are printed.

Now I NOT know what it is, haha ! = NOT. Anyways it's late and that was my first programmers joke ever and a bad one I am sure : )
I am afraid that this is about as good as programming jokes get :D
 

Sayer

macrumors 6502a
Jan 4, 2002
981
0
Austin, TX
For future reference:

! - NOT, this must be false to continue
!= - NOT equal to N, for comparing non-boolean (numerical) values
== - EQUAL to N, for comparing non-boolean (numerical) values
&& - AND, this AND that = both must be true to continue
|| - OR, this OR that = one or both must be true to continue

% - modulus or mod, aka division returning only the remainder e.g. 17 mod 5 = 2 (2 being the remainder from 17 divided by 5).

For bitwise operations you use only one & and | for "AND" and "OR."

And then if you are doing C++ you use a lot of reference passing which has a & precede an argument in a function call. This just means "here is the address to the beginning of the object or data" which is typ. only 32 bits (4 bytes) in size instead of sending the entire chunk of data as an argument, which can be many, many bytes large.

Code:
	OSErr			theErr = noErr;
	CInfoPBRec 		pBlock; // this is a very large data struct

	memset(&pBlock, 0, sizeof(pBlock));

	...

	theErr = PBSetCatInfoSync(&pBlock);

	if (theErr != noErr) return theErr;
 

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
So ! this must be false? the (!validInput) was of type boolean which could only result in a True or False answer.

Here is another example from the book,
Code:
 if (num ! = 12)
So in this example he is saying if it is NOT equal to 12, is my understanding of that right?

So in the !validInput example it would only execute if it is NOT TRUE or would that convert a TRUE to a FALSE boolean type?

thank,

-Lars
 

Doctor Q

Administrator
Staff member
Sep 19, 2002
37,287
3,373
Los Angeles
larswik said:
So ! this must be false? the (!validInput) was of type boolean which could only result in a True or False answer.

Here is another example from the book,
Code:
 if (num ! = 12)
So in this example he is saying if it is NOT equal to 12, is my understanding of that right?

So in the !validInput example it would only execute if it is NOT TRUE or would that convert a TRUE to a FALSE boolean type?
Yes, when ! precedes an expression it changes TRUE to FALSE and FALSE to TRUE.

The "!=" operator goes between two values you want to compare. In particular, if (num ! = 12) ... means to do the ... part when num is not equal to 12.

If you think about it, you'll see that these two produce the same result:
Code:
if ( num != 12 ) ...

if ( ! ( num == 12 ) ) ...
The first uses the "not equal" comparison operator. The second uses the logical "not" operator on the result of the "equal" comparison. Said in English, the first is "num is not 12" and the second is "it is not the case that num is 12". So in English they mean the same thing too.
 

iMeowbot

macrumors G3
Aug 30, 2003
8,636
0
larswik said:
So ! this must be false? the (!validInput) was of type boolean which could only result in a True or False answer.
That is correct.

Here is another example from the book,
Code:
 if (num ! = 12)
So in this example he is saying if it is NOT equal to 12, is my understanding of that right?
Right.

So in the !validInput example it would only execute if it is NOT TRUE or would that convert a TRUE to a FALSE boolean type?
The first. It is only a test in this case. An assignment statement like:

validInput = !validInput;

…would be needed for ! to change the value.
 

dukebound85

macrumors P6
Jul 17, 2005
18,105
1,287
5045 feet above sea level
iMeowbot said:
The world is filled with books like that! I do wonder how many textbooks are ever shown to people who don't know the subject yet, to see if they make any sense before they are printed.


I am afraid that this is about as good as programming jokes get :D

lol or there are 10 types of people in this world, those who understand binary and those who dont hahahaha now im a geek too
 

Krevnik

macrumors 68040
Sep 8, 2003
3,313
676
If things make sense, great. Although a lot of this might be a little easier to understand/use if you read up a little on Boolean Algebra. It isn't required reading, and I am pretty sure that a few of the senior programmers in my product area never took classes, but I found that a lot of the logical operators made more sense and were more useful pieces of my programming knowledge once I read up a bit on it. Plus, it helps formalize a bit of the boolean logic used so heavily in expressions. The real only difference is the syntax in C for boolean operators are different then when you write it on paper.
 

savar

macrumors 68000
Jun 6, 2003
1,952
0
District of Columbia
larswik said:
So in the !validInput example it would only execute if it is NOT TRUE or would that convert a TRUE to a FALSE boolean type?

thank,

-Lars
! is an operator, just like +, -, *, and /. The ! operator inverts a boolean, so true becomes false, but false also becomes true. An expression like (num == 12) evaluates to a boolean result, so that !(num == 12) is the same as (12 != num).

The if test only looks at the final result after evaluating all expressions inside the parentheses. If this result is true, then the following commands are executed. If it evaluates to false then those commands are skipped.