Re: Java programming question. Boolean
The while loop condition only requires and expression that returns a boolean. There are many ways to have that happen, and not all of them involve an equals/not equals sign. This is a simple case, where just the variable's name gives you a boolean value, which the while loop checks for true/false. You can also do things like negate it, use a boolean and/or on it with other expressions, or return a boolean from a function (eg, while(iter.hasNext()).
The situation in C is slightly different. C has no innate boolean class, so the while loop condition looks for integers. If the integer is 0, the condition evaluates to false. Otherwise, it is true. C implicitly "casts" (reinterprets) a lot of things to int, which lets you put a few more things in the condition expression. Any numeric type checks whether it equals 0, and any other type casts to int and checks if that value is 0. In most cases this means checking if a pointer is null or not. C++ has a similar situation for primitive types because it is (mostly) a superset of C.
Last edited by schauerlich; August 1st, 2012 at 03:39 PM.
Posting code? Use the [code] or [php] tags.
I don't care, I'm still free. You can't take the sky from me.
Bookmarks