• Post Reply Bookmark Topic Watch Topic
  • New Topic

Boolean Expressions  RSS feed

 
Harish Shivaraj
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im trying to do this



However if statement requires a return of type boolean. So im forced to this instead



Is there a way to achieve the former method, without typecasting, if you had to typecast how do you do it?

Thanks
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you aware of the modulus operator "%" ? The usual way to check whether a number is even (I assume "event" is just a typo) is to check if the remainder of a division by 2 is 0 or 1.
 
Harish Shivaraj
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed ofcourse it must be % operator. But however Im still forced to use the == operator to check the whether return/remained is 0. Apparently the if statement can only handle Boolean type. Can I typecast int to Boolean?
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you can't do that.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you need to cast anything? The == operators returns a boolean.
 
Harish Shivaraj
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Why would you need to cast anything? The == operators returns a boolean.


Sure, Im was more after, if I can do this like in C



Which you cant apparently in Java.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is not C. Never has been. Never will be.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harish Shivaraj wrote:Sure, Im was more after, if I can do this like in CWhich you cant apparently in Java.

As others have said, no you can't. What you can do, however, is:which strikes me as a lot easier to read anyway; and it's highly possible that the compiler may inline it for you.

Winston
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most older languages like C used 0 = false and other values e.g. 1, -1, anything non‑zero for true. More modern languages introduced a boolean type (I think C++ does) and better still they prevented one from using integers instead of booleans (Java® does, I think C++ doesn't however). So in C/C++ you have to write
if (NULL == x)...
but you never write such things in Java®.
About the only vestige of 0 = false in Java® is the %b tag for printf.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Most older languages like C used 0 = false and other values e.g. 1, -1, anything non‑zero for true.

Which led to monstrosities like:
  if (! strcmp(str1, str2)) { ...

meaning "if str1 and str2 are equal".

@Harish: if you've just come from C, it takes a bit of getting used to - but trust me, you'll learn to love 'em (booleans).

Winston
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . .
  if (! strcmp(str1, str2)) { ...
. . .
The version of that in Forth would be even worse. You have to use 0= because NOT constitutes one's complement not negation
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Which led to monstrosities like:
  if (! strcmp(str1, str2)) { ...

meaning "if str1 and str2 are equal".




But this is very expressive and far from being monstrous:

 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Java is not C. Never has been. Never will be.


In the beginning it was without form and void, and darkness and Cobol were over the face of the deep.
Then K&R said "Let there be B" (pun intended) and there was B and they saw B was good.
And there was evening and there was morning.
And K said “Let there be pointers in the midst of the data and pointer arithmetics.”
And R said "Let there be C".
And it was so.
And K&R saw C was good.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!