Java
  Home arrow Java arrow Page 3 - Conditionals, Expressions and Other Ja...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Conditionals, Expressions and Other Java Operators
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2007-10-23

    Table of Contents:
  • Conditionals, Expressions and Other Java Operators
  • Expressions
  • Climbing the Old If Else If Ladder
  • The Switch Statement

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    Iron Speed
     
    ADVERTISEMENT

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

    Conditionals, Expressions and Other Java Operators - Climbing the Old If Else If Ladder
    (Page 3 of 4 )

    Since the beginning of time man has been asking these important questions: Can a homo sapien beat up a homo erectus? Can a man beat a saber-toothed tiger? Can a saber-toothed tiger defeat a giant sloth? Will Jessie Jackson ever become president? Did Bill Clinton ever have relations with that woman? 

    When you have a lot of Ifs, you need to build yourself a good old fashioned If Else If statement. Look at the beautiful and pertinent one below:


    class SuperHero {

    public static void main(String[]) {

    int hulk = 9;

    int daredevil = 10;

    int batman = 11;

    int wolverine = 8;

    String winner = "Whoever"

     

     

    if(daredevil > hulk && daredevil > batman && daredevil > wolverine)

    winner = "Daredevil is the winner!";

    else if(hulk > daredevil && hulk > batman && hulk > wolverine)

    winner = "The Incredible Hulk is the winner!";

    else if(batman > daredevil && batman > hulk && batman > wolverine)

    winner = "Batman is the winner!!";

    else if(wolverine > hulk && wolverine > daredevil && wolverine > batman)

    winner = "Wolverine is the winner!";


    else winner = "Nobody wins!";


    System.out.println(winner);

     

    }

    }

    The above code is for a battle royal amongst the super heroically inclined. Each hero is assigned a numeric value and then compared to each other hero. Whichever hero has the highest value gets stored in the winner category. In this case, the winner is Batman, and the following prints to the screen:

      Batman is the winner!

    If no one met the criteria (had a higher value than the other three super heroes), then the following would have been printed to the screen:

      Nobody wins!

    More Java Articles
    More By James Payne


       · This article covers expressions in Java, as well as conditionals such as the If...
     

       

    JAVA ARTICLES

    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup
    - Using RPC-Style Web Services with J2EE
    - Integrating XML with J2EE
    - Taming Tiger: Concurrent Collections
    - Combating the ‘Object Crisis’

    Iron Speed



    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway