Java
  Home arrow Java arrow Page 2 - 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 
eWeek
 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

    Anyone looking for a way to modernize legacy data or easily migrate to a more cost-effective database without sacrificing functionality will benefit from this seminar. View the Intro to Advantage Database Server now!

    Conditionals, Expressions and Other Java Operators - Expressions
    (Page 2 of 4 )

    One last thing before we proceed: when it comes to operator precedence, there is a way to change it. Simply use parentheses(). When your program evaluates your statement, it will execute the part of your equation in parentheses() first. Like so...


    a = 5 * (1 + 2);

    This would result in the value of a being 15, whereas had we written the statement this way:

    a = 5 * 1 + 2;

    we would have received a result of 7. Because the brackets were not there, the program simply multiplied 5 times 1 (which equals 5), then added 2, for a total of seven. In the previous example, since the (1+2) was in brackets, it added those together first (equaling 3) and then multiplied that number by 5 (for a result of 15).

    Statements

    There are three types of statements in Java: Selection, Iteration, and Jump. Let's take a look at each one.

    Selection Statements

    There are two types of Selection Statements in Java. The first, which we previewed in the prior tutorial, is the If Statement. In simple terms, it states that if this happens, do that.


    int james = 10;

    int you = 2;


    if (james > you) you = 0;

    else james = 0;

    See what happens there? If the value of James is greater than the value of You, then your value becomes 0. If your value was greater than mine, my value would become 0.

    Sometimes you need to do more than one if statement within an if statement. Consider the code below:


    int james = 10;

    int you = 2;

    int your_daddy =3;


    if (james > You) {

    if (james > your_daddy) your_daddy = 0;

    else james = 0; //refers to the closest if

    }

    else james = 0; //refers to the original if

    In the code above, the program asks the question: Is James better than You? If so, it goes to the next if statement and asks if James is better than your daddy. If he is, it sets your daddy's value at 0. If not, it sets James value at 0.

    If James value had been less than yours, it would have skipped the comparison to your father, and changed James value to 0.

    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
     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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