Java
  Home arrow Java arrow Page 3 - Java Statements
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

Java Statements
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2007-10-24


    Table of Contents:
  • Java Statements
  • While Loop
  • Do-While
  • For Loop
  • To Be Continued...

  • 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


    Java Statements - Do-While
    ( Page 3 of 5 )

    Part of being a bratty kid is testing your limits. How many times can I get a cookie out of the cookie jar before my parents notice; how many times can I put the dog in a pillow case and swing it in the air before it bites me?

    As I said previously, if the value of the timer was not greater than 0, the program would have skipped the loop entirely. Sometimes however, we want the program to execute the loop at least once. For that, we use the Do-While.


    Class CountDown {

    public static void main(String args[]) {

    int timer = 10;


    do {

    System.out.println(“T minus “ + timer);

    timer--;

    } while(timer > 0);

    }

    }

    The sample above will once again result in the following:

      T minus 10

      T minus 9

      T minus 8

      T minus 7

      T minus 6

      T minus 5

      T minus 4

      T minus 3

      T minus 2

      T minus 1

    The difference however is that even if timer had not been greater than 0, it still would have printed the following:

      T minus 10

    Let's make a program where you get to select your own punishment for being bad.


    class YourBeating {

    public static void main(String args[])

    char punishment;


    do {

    System.out.println(“You've been very bad.”);

    System.out.println(“Please select your punishment:”);

    System.out.println(“1: A firm talking to.”);

    System.out.println(“2: A spanking.”);

    System.out.println(“3: Forced to watch the View all day.”);

    System.out.println(“4: Be pitied by the poet Mr. T.n”);

    favorite = (char) System.in.read();

    } while( favorite < '1' || favorite > '4');


    System.out.println(“n”);


    switch(favorite) {

    case '1':

    System.out.println(“Prepare to be bored! Let the talking commence!”);

    break;

    case '2':

    System.out.println(“I'll spank you so hard your He-Man underoos will turn into Strawberry Shortcake!”);

    break;

    case: '3':

    System.out.println(“Forced to watch the View...A worse punishment I could not imagine.”);

    break;

    case '4':

    System.out.println(“Mr. T has pitied your greatly!”);

    break;


    }

    }

    }

    The above code lists a bunch of possible punishments (four in total). If the user presses any key other than 1,2,3, or 4, the program will simply loop again, giving them their four options. If they chose say punishment four, the following would print out to the screen:

      Mr. T has pitied you greatly!



     
     
    >>> 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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek