Java
  Home arrow Java arrow Page 3 - Conditionals, Expressions and Other Java Operators
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 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? 
Google.com  
JAVA

Conditionals, Expressions and Other Java Operators
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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

     
     
    ADVERTISEMENT


    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
     

       

    JAVA ARTICLES

    - Exception Handling Techniques in Java
    - More About Multithreading in Java
    - The Basics of Multiple Threads in Java
    - Data Access Using Spring Framework JDBC
    - New Object Initialization in Java
    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek