Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to tell if a number is even or odd, if statement

 
Brandi Love
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I tell whether or not a given integer is even or odd using java? Also, can I put an if statement within a while loop?
[ November 02, 2003: Message edited by: Brandi Love ]
 
David Crossett
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope I'm not just blowing hot air, but this should be easy:

Hope that helps!
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can I put an if statement within a while loop?

You can put anything you want inside a while loop.
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simplify David's code to:

In fact, this is a great place to use the syntax:
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

That may be legal, but it isn't read-able.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about:
isEven = number%2==0?true:false;

I actually found code similar to this in a program I was debugging.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Wayne]: isEven = number % 2 == 0;
[Wililam]: That may be legal, but it isn't read-able.

It's not? Ummm... how about now?
boolean isEven = (number % 2 == 0);
[Tom]: isEven = number%2==0?true:false;
OK, now that one's pretty funny.
 
S Perreault
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by William Barnes:

That may be legal, but it isn't read-able.

Actually writing code like this (instead of the first example) is one of the first ways to separate yourself from the newbie programmers. It is as readable as the first example, and much more concise.
Perogi.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic