SunQuest
 
       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  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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? 
JAVA

Java Statements
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 7
    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:
      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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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


       · This article discusses Java Statements, like the If statement and Loops (such as the...
     

       

    JAVA ARTICLES

    - 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...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup





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