wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes how to tell if a number is even or odd, if statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to tell if a number is even or odd, if statement" Watch "how to tell if a number is even or odd, if statement" New topic
Author

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

Brandi Love
Ranch Hand

Joined: Sep 19, 2003
Posts: 133
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

Joined: Feb 05, 2003
Posts: 102
I hope I'm not just blowing hot air, but this should be easy:

Hope that helps!


David Crossett
-nothing important to say, but learnin' plenty-
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

can I put an if statement within a while loop?

You can put anything you want inside a while loop.


Please ignore post, I have no idea what I am talking about.
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
Simplify David's code to:

In fact, this is a great place to use the syntax:
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986


That may be legal, but it isn't read-able.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
How about:
isEven = number%2==0?true:false;

I actually found code similar to this in a program I was debugging.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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.


"I'm not back." - Bill Harding, Twister
S Perreault
Ranch Hand

Joined: Oct 29, 2003
Posts: 37
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to tell if a number is even or odd, if statement