Java
  Home arrow Java arrow 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
     
     
     
    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
    (Page 1 of 4 )

    In our last article we left off with Operators. Since then a strange time fluctuation has occurred, and that is exactly where we pick up. So keep reading to learn about exotic things like conditionals, expressions, and more.

    As I recall, by some fluke, you had ended up with more muscles than me (*cough* steroids *cough*) and I put a call into my buddy at the Steroid Usage is a No No Board. To determine if you were a liar, they would have to ask you a series of Boolean questions: True or False -- did you do steroids? Yes or No -- Are you Barry Bonds?

    Booleans are black and white; for them, there is no in between. It's either true or false, yes or no, on or off. We will get into how to use them in programming a bit later on. For now take comfort in the fact that they exist.

    Conditional Operators

    Sometimes you need to know if more than one criteria has been met, or if one of the two criteria has been met. To do that, use conditional operators.


    class IamBetter {


    public static void main(String[] args) {

    int my_muscles = 10;

    int your_muscles = 10;

    int my_looks = 10;

    int your_looks =10;

    if((my_muscle > your_muscle) && (my_looks > your_looks)) System.out.println("I really AM better than you in every way!");

    In the above example, we are asking the computer if my looks and muscles are better than yours. As you can see by the values, they are. Because both criteria are met, the program will print to the screen: I really AM better than you in every way!

    If only one criteria had been met, it would have printed nothing.

    Let's say again, that because of your heavy steroid use and because my Pilates exercise builds lean muscles instead of those stupid looking ones that you have, that you appear to have more muscles than me. It wouldn't matter; I can make the program say that I am still better than you.


    class IamBetter {


    public static void main(String[] args) {

    int my_muscles = 10;

    int your_muscles = 11;

    int my_looks = 10;

    int your_looks =10;

    if((my_muscle > your_muscle) || (my_looks > your_looks)) System.out.println("I really AM better than you in every way!");

    Now that I have changed the criteria so that only one match will suffice, it will still print that I really AM better than you in every way! Again, if neither criteria was true, it would have printed nothing.

    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’




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