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

    Sometimes you feel like some nuts. Sometimes you don't. Sometimes you also don't feel like writing a bunch of If-Elses all over the place. Well slacker, meet my little friend the Switch Statement.


    Public static void main(String args[]) {

    for(int launch_time = 0; launch_time < 11; launch_time++)

    switch(launch_time) {

    case 0:

    case 1:

    case 2:

    case 3:

    case 4:

    System.out.println("Shuttle launch in less than five seconds");

    break;

    case 5:

    case 6:

    case 7:

    case 8:

    case 9:

    System.out.println("Shuttle launch in less than ten seconds");

    break;

    default:

    System.out.println("Shuttle launch in less than 20 seconds");

     

    }

    }

    }


    The above code (while it technically counts down backwards) will print the following to the screen:

      Shuttle launch in less than five seconds

      Shuttle launch in less than five seconds

      Shuttle launch in less than five seconds

      Shuttle launch in less than five seconds

      Shuttle launch in less than five seconds

      Shuttle launch in less than ten seconds

      Shuttle launch in less than ten seconds

      Shuttle launch in less than ten seconds

      Shuttle launch in less than ten seconds

      Shuttle launch in less than ten seconds

      Shuttle launch in less than 20 seconds

      Shuttle launch in less than 20 seconds

    If you had used the If-else statements you would have had to compare the value of launch_time each time to print out the result (ie; is launch_time greater than 1? is launch_time greater than 2, etc).

    It is possible to nest Switch statements as well, but that is beyond the scope of this tutorial. In fact, anything more is beyond the scope of this tutorial, as we have come to the end of it.

    Thanks for sticking around. In the next tutorial, I will go over the remaining statements (the Iteration and the Jump). So, hope to see you then.

    Till next time...



     
     
    >>> 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